永远不会选中警报对话框中的自定义单选按钮
Custom Radio Buttons in Alert Dialog are never checked
我有一个带有单选按钮的自定义 AlertDialog,它会在单击文本视图时打开。除了单选按钮从未显示为 "checked" 之外,一切都按预期工作。每当我打开 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>
我的警报对话框:
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();
}
}
});
每次调用 therapistDialog
方法时,您都不会在打开时恢复对话框的状态并分配一个新的 AlertDialog
实例。
尝试检查 AlertDialog
view 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);
每次调用方法 therapistDialog()
都会创建一个新的 alertDialogBuilder 实例。您必须在全局范围内仅创建一次 alertDialogBuilder 实例,
然后打电话给
final AlertDialog alertDialog = dialogBuilder.show();
在你的方法中。
注意:这没有经过测试,但应该可以工作,并且还会保持您的复选框状态
我有一个带有单选按钮的自定义 AlertDialog,它会在单击文本视图时打开。除了单选按钮从未显示为 "checked" 之外,一切都按预期工作。每当我打开 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>
我的警报对话框:
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();
}
}
});
每次调用 therapistDialog
方法时,您都不会在打开时恢复对话框的状态并分配一个新的 AlertDialog
实例。
尝试检查 AlertDialog
view 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);
每次调用方法 therapistDialog()
都会创建一个新的 alertDialogBuilder 实例。您必须在全局范围内仅创建一次 alertDialogBuilder 实例,
然后打电话给
final AlertDialog alertDialog = dialogBuilder.show();
在你的方法中。
注意:这没有经过测试,但应该可以工作,并且还会保持您的复选框状态