簡體   English   中英

如何在android studio中從意圖設置單選按鈕檢查

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM