首页 > 解决方案 > 如何在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>

但它没有被选中。

标签: javaandroidandroid-studio

解决方案


当您使用 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()删除不必要的空格。如果没有必要,您可以避免。

更新

这是完整的功能代码。

MainActivity.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);
            }
        });
    }
}

activity_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>

SecondActivity.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>

推荐阅读