[英]how to set radio button check from intent in android studio
我想通过 Intent 获取上一个活动的值来设置选中的单选按钮。 但它不起作用。
这是我从 MyProfileActivity 发送到 EditProfileActivity 的意图。
Intent intent = new Intent(getContext(), EditProfileActivity.class);
intent.putExtra("nameResult", nameResult);
intent.putExtra("emailResult", emailResult);
intent.putExtra("phoneResult", phoneResult);
intent.putExtra("genderResult", genderResult);
intent.putExtra("dobResult", dobResult);
startActivity(intent);
这是我从 MyProfileActivity 收到的意图,
Intent intent = getIntent();
binding.updateName.setText(intent.getStringExtra("nameResult"));
binding.updateEmail.setText(intent.getStringExtra("emailResult"));
binding.updateNumber.setText(intent.getStringExtra("phoneResult"));
String selectedRadioTxt = getIntent().getStringExtra("genderResult");
binding.updateDob.setText(intent.getStringExtra("dobResult"));
我想设置选中的单选按钮。 我还使用 toast 检查了该值,并确保该值是正确的。 现在价值来了男性。 但是在此代码之后未选择单选按钮男性。
String male = "male";
String female = "female";
RadioButton maleButton = findViewById(R.id.male);
RadioButton femaleButton = findViewById(R.id.female);
if(selectedRadioTxt.equals(male)){
maleButton.setChecked(true);
Toast.makeText(this, male, Toast.LENGTH_SHORT).show();
}
else if(selectedRadioTxt.equals(female)){
femaleButton.setChecked(true);
Toast.makeText(this, female, Toast.LENGTH_SHORT).show();
}
这是xml文件,
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout5"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".EditProfile" >
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/editDp"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="50dp"
android:src="@drawable/dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/updateName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="30dp"
android:background="@drawable/custom_edit_text"
android:ems="10"
android:fontFamily="@font/amarante"
android:hint="@string/full_name"
android:importantForAutofill="no"
android:inputType="textPersonName"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editDp" />
<EditText
android:id="@+id/updateEmail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/custom_edit_text"
android:ems="10"
android:fontFamily="@font/amarante"
android:hint="@string/email"
android:importantForAutofill="no"
android:inputType="textEmailAddress"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="@+id/updateName"
app:layout_constraintStart_toStartOf="@+id/updateName"
app:layout_constraintTop_toBottomOf="@+id/updateName" />
<TextView
android:id="@+id/updateNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/custom_edit_text"
android:ems="10"
android:textSize="18sp"
android:fontFamily="@font/amarante"
android:hint="@string/mobile_number"
android:importantForAutofill="no"
android:inputType="phone"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="@+id/updateEmail"
app:layout_constraintStart_toStartOf="@+id/updateEmail"
app:layout_constraintTop_toBottomOf="@+id/updateEmail" />
<TextView
android:id="@+id/updateDob"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/custom_edit_text"
android:ems="10"
android:fontFamily="@font/amarante"
android:hint="@string/date_of_birth"
android:importantForAutofill="no"
android:inputType="textPersonName"
android:padding="10dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="@+id/updateGender"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/updateGender"
app:layout_constraintTop_toBottomOf="@+id/updateGender" />
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="72dp"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:background="@drawable/bg_button"
android:fontFamily="@font/aclonica"
android:hapticFeedbackEnabled="true"
android:text="@string/update"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold"
app:backgroundTint="@null"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/updateDob"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/updateDob"
app:layout_constraintTop_toBottomOf="@+id/updateDob"
app:layout_constraintVertical_bias="0.451" />
<RadioGroup
android:id="@+id/updateGender"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/custom_edit_text"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="@+id/updateNumber"
app:layout_constraintStart_toStartOf="@+id/updateNumber"
app:layout_constraintTop_toBottomOf="@+id/updateNumber">
<RadioButton
android:id="@+id/male"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="@font/amarante"
android:onClick="checkButton"
android:text="@string/male" />
<RadioButton
android:id="@+id/female"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="@font/amarante"
android:onClick="checkButton"
android:text="@string/female" />
<RadioButton
android:id="@+id/other"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="@font/amarante"
android:onClick="checkButton"
android:text="@string/other" />
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
但它没有被选中。
None 被检查,因为String.valueOf(R.id.male)
返回的值与您在 if 语句中将其等同的值不同。
String.valueOf(R.id.male)
是一个刚刚转换为字符串的 int。 含义selectedRadioTxt
永远不能等于String.valueOf(R.id.male) or String.valueOf(R.id.female)
在其他病房:例如。 selectedRadioTx = "male"
but String.valueOf(R.id.male) ="some Int vale eg 20133321"
解决方案将您的 if 语句更改为:
if(selectedRadioTxt.trim().equals("male")){
binding.male.setChecked(true);
Toast.makeText(this, "male", Toast.LENGTH_SHORT).show();
}
else if(selectedRadioTxt.trim().equals("female")){
binding.female.setChecked(true);
Toast.makeText(this, "female", Toast.LENGTH_SHORT).show();
}
请务必注意 Strings male != Male 和 Female != "Female" 上的大写字母。 根据 if 语句中的解决方案,确保您从意图selectedRadioTx
获得的值等于男性或女性。
您可以在意图之前检查第一个活动中的性别结果值吗
由于您正在使用 RadioGroup,请尝试
RadioGroup gender = findViewById(R.id.updateGender);
if(selectedRadioTxt.trim().equals(male.trim())) {
gender.check(R.id.male);
} else {
gender.check(R.id.female);
}
我使用trim()
删除了不必要的空格。 如果没有必要,您可以避免。
更新
这是全功能代码。
主活动.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String genderResult = "male";
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("genderResult", genderResult);
startActivity(intent);
}
});
}
}
活动_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
第二个Activity.java
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
String male = "male";
String female = "female";
String selectedRadioTxt = getIntent().getStringExtra("genderResult");
RadioGroup gender = findViewById(R.id.gender);
if(selectedRadioTxt.equals(male)) {
gender.check(R.id.male);
} else if(selectedRadioTxt.equals(female)) {
gender.check(R.id.female);
}
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<RadioGroup
android:id="@+id/gender"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioButton
android:id="@+id/male"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/female"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.