这是我的对话框
public class CustomDialogClass extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button no;
CheckBox yes;
boolean p;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (CheckBox) findViewById(R.id.checkBox1);
no = (Button) findViewById(R.id.btn_no);
no.setOnClickListener(this);
yes.setChecked(false);
yes.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.checkBox1:
yes.setChecked(true);
break;
case R.id.btn_no:
dismiss();
break;
default:
break;
}
}
}
现在,我打开对话框并选中复选框,单击按钮并关闭对话框。但是当我再次打开它时,该复选框再次未选中。我该怎么办?
最佳答案
不再推荐您使用对话框的方式!您应该考虑使用 DialogFragment。
您丢失选中状态的问题是因为当您再次打开对话框时会重新创建该对话框。
请参阅此 DialogFragment 方法示例 http://developer.android.com/reference/android/app/DialogFragment.html .您将看到您可以将参数传递给对话框。
对于解决方案,我建议当您关闭对话框时,将所选值传回宿主 Activity ...当对话框应该重新打开时,您只需将参数传递给对话框,以便对话框设置其默认选择。
编辑:
既然你想显示复选框,我会采用并调整 示例代码来自 http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList 对于复选框。用起来还是很方便的 AlertDialog.Builder。
通过覆盖 onCreateDialog 方法。您可以通过以下方式传递所选项目的列表 Bundle 作为 bool 数组,然后用于 setMultiChoiceItems。
public class CheckBoxAlertDialogFragment extends DialogFragment { public static CheckBoxAlertDialogFragment newInstance(boolean[] checkedItems) { CheckBoxAlertDialogFragment frag = new CheckBoxAlertDialogFragment(); Bundle args = new Bundle(); args.putBooleanArray("checkedItems", checkedItems); frag.setArguments(args); return frag; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final boolean[] checkedItems = getArguments().getBooleanArray("checkedItems"); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Set the dialog title builder.setTitle(R.string.pick_toppings) // Specify the list array, the items to be selected by default (null for none), // and the listener through which to receive callbacks when items are selected .setMultiChoiceItems(R.array.toppings, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkedItems[which] = isChecked; } }) // Set the action buttons .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // User clicked OK, so save the checkedItems results somewhere // or return them to the component that opened the dialog //... } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { //... } }); return builder.create(); } }
在您的 Activity 中,您应该有一个变量,例如名为 checkedItems,类型为 boolean[] 的某处保存复选框状态。您可以使用以下命令打开对话框:
void showDialog() { DialogFragment newFragment = CheckBoxAlertDialogFragment.newInstance(checkedItems); newFragment.show(getFragmentManager(), "dialog tag"); }
关于android - 使用复选框实现对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17859514/