Vip*_*mar 1 checkbox android dialog
这是我的对话框
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我打开对话框并选中复选框,单击按钮并关闭对话框.但是当我再次打开它时,复选框再次被取消选中.我该怎么办?
不建议您使用对话框的方式!您应该考虑使用DialogFragment.
丢失已检查状态的原因是因为再次打开对话框时会重新创建对话框.
请参阅DialogFragment方法的示例http://developer.android.com/reference/android/app/DialogFragment.html.您将看到可以将参数传递给对话框.
对于解决方案,我建议您在关闭对话框时将所选值传递回托管活动...当应重新打开对话框时,只需将参数传递给对话框,以便对话框设置其默认选择.
编辑:
由于您希望显示复选框,我将采用http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList中的示例代码 来复选框.使用AlertDialog.Builder仍然很方便.
通过覆盖onCreateDialog方法将Builder包装到DialogFragment中.您可以通过Bundle作为布尔数组传递所选项的列表,然后使用is用于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();
}
}
Run Code Online (Sandbox Code Playgroud)在你的活动中,你应该有一个变量,例如名为checkedItems,类型为boolean []的地方,它保存了复选框状态.您可以使用以下命令打开对话框:
void showDialog() {
DialogFragment newFragment = CheckBoxAlertDialogFragment.newInstance(checkedItems);
newFragment.show(getFragmentManager(), "dialog tag");
}
Run Code Online (Sandbox Code Playgroud)