从不检查警报对话框中的自定义单选按钮

时间:2016-06-24 11:27:23

标签: android android-alertdialog android-radiobutton

我有一个带有单选按钮的自定义AlertDialog,点击文本视图时会打开。除了单选按钮从未显示为"已检查"外,所有内容都按预期工作。每当我打开AlertDialog时,所有按钮都将被取消选中。选择按钮并再次打开AlertDialog后,再次取消选中按钮。

我的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:padding="15dp">

<RadioGroup
    android:id="@+id/rg_gender"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_marginRight="30dp"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="10dp"
    android:orientation="vertical"
    android:layout_gravity="center_horizontal">

    <RadioButton
        android:id="@+id/buttonMale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/therapist_male"
        />

    <RadioButton
        android:id="@+id/buttonFemale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/therapist_female"
        />

    <RadioButton
        android:id="@+id/buttonEither"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/therapist_either"
        />
</RadioGroup>
</LinearLayout>

My AlertDialog:

private void therapistDialog() {

    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.gender_dialog, null);
    dialogBuilder.setView(dialogView);
    dialogBuilder.setNegativeButton(getResources().getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    final AlertDialog alertDialog = dialogBuilder.show();

    final RadioGroup rg = (RadioGroup) dialogView.findViewById(R.id.rg_gender);
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.buttonMale){
                tvGender.setText(getResources().getString(R.string.therapist_male));
                therapistSex = 0;
                mMassage.setTherapistSex(therapistSex);
                male.setChecked(true);
                alertDialog.dismiss();
            } else if (checkedId == R.id.buttonFemale){
                tvGender.setText(getResources().getString(R.string.therapist_female));
                therapistSex = 1;
                mMassage.setTherapistSex(therapistSex);
                alertDialog.dismiss();
            } else if (checkedId == R.id.buttonEither){
                tvGender.setText(getResources().getString(R.string.therapist_either));
                therapistSex = 2;
                mMassage.setTherapistSex(therapistSex);
                alertDialog.dismiss();
            }
        }
    });

2 个答案:

答案 0 :(得分:0)

每次调用AlertDialog方法时,您不会在打开时恢复对话状态并指定therapistDialog的新实例。
尝试检查AlertDialog查看init上的按钮 添加

int buttonToCheck = mMassage.getTherapistSex();
RadioGroup radioGroup = dialogView.findViewById(R.id.rg_gender);
int buttonId = radioGroup.getChildAt(buttonToCheck).getId();
radioGroup.check(radioGroup.getChildAt(buttonToCheck).getId());

在调用dialogBuilder.setView(dialogView);

之前

答案 1 :(得分:0)

每次调用方法therapistDialog()时都会创建alertDialogBu​​ilder的新实例。您必须全局创建一次alertDialogBu​​ilder实例,

然后致电

你方法中的

final AlertDialog alertDialog = dialogBuilder.show();

注意:这未经过测试,但这应该有效并且还会保持您的复选框状态

相关问题