java如何使用分级栏在活动之间传递信息
好吧,我对安卓和整个应用程序制作都很陌生,但我只是在做一个普通的应用程序评级,只是为了练习。以下是详细信息,我在主活动上只有两张普通图片,每个图片旁边都有一个“速率”按钮,下面都有一个文本视图,因此当您单击“速率”按钮时,它会将您带到第二个活动,该活动有一个“速率”栏。这是我在查看评级栏时遇到的问题。我只是将评级显示为祝酒词,但我希望能够将该评级显示在我的主要活动的文本视图中,它带来的问题比我想象的要多,因此我正在寻求帮助
这是我主要活动的代码
package com.example.brent.appmanagement;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.view.View;
import 安卓.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingapp1();
ratingapp2();
getrating();
}
private void getrating(){
}
private void ratingapp1(){
Button rateapp1 = findViewById(R.id.rateapp1);
rateapp1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, firstapp.class));
}
});
}
private void ratingapp2(){
Button rateapp2 = findViewById(R.id.rateapp2);
rateapp2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, secondapp.class));
}
});
}
}
这是带有评级条的活动代码
public class firstapp extends AppCompatActivity {
public RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firstapp);
ratingBar = findViewById(R.id.ratingBar);
return1();
}
public void rateMe(View view){
Toast.makeText(getApplicationContext(),
String.valueOf(ratingBar.getRating()),
Toast.LENGTH_LONG).show();
}
private void return1(){
Button returntomain1 = findViewById(R.id.returntomain1);
returntomain1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(firstapp.this, MainActivity.class));
}
});
}
}
我也有XML,我不知道你是否需要看,但它在这里
主要活动
<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.brent.appmanagement.MainActivity">
<Button
安卓:id="@+id/rateapp1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginEnd="96dp"
安卓:layout_marginTop="124dp"
安卓:text="Rate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
安卓:id="@+id/rateapp2"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="176dp"
安卓:layout_marginEnd="96dp"
安卓:text="Rate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ImageView
安卓:id="@+id/imageView3"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="96dp"
安卓:layout_marginTop="112dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<ImageView
安卓:id="@+id/imageView4"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="164dp"
安卓:layout_marginStart="96dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
安卓:id="@+id/savedrating1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="96dp"
安卓:layout_marginTop="12dp"
安卓:text=""
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView3" />
<TextView
安卓:id="@+id/savedrating2"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="96dp"
安卓:layout_marginTop="12dp"
安卓:text=""
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView4" />
</安卓.support.constraint.ConstraintLayout>
第二个活动呢
<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.brent.appmanagement.firstapp">
<Button
安卓:id="@+id/returntomain1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="8dp"
安卓:layout_marginStart="8dp"
安卓:text="Return to main menu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
安卓:id="@+id/rate_me"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="8dp"
安卓:layout_marginTop="8dp"
安卓:text="Rate Me"
安卓:textSize="18dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RatingBar
安卓:id="@+id/ratingBar"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/rate_me"
安卓:layout_marginStart="8dp"
安卓:layout_marginTop="8dp"
安卓:numStars="5"
安卓:stepSize="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rate_me" />
<Button
安卓:id="@+id/submit"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/ratingBar"
安卓:layout_marginStart="8dp"
安卓:layout_marginTop="8dp"
安卓:onClick="rateMe"
安卓:text="Submit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ratingBar" />
</安卓.support.constraint.ConstraintLayout>
我想让firstactivity内的评级栏中的评级显示在我的主活动内的textview savedrating1中
提前感谢大家的帮助
# 1 楼答案
对于基本值,您可以使用键值对将它们填充到intent中
Intent intent =new Intent(); String myValue="something";intent.putExtra("keyMyValue",myValue);startActivity(intent);
对于传递自定义对象,请使用parcelable并将parcelable填充到intent中,如上所述or here