实现与复选框对话框 [英] Implementing Dialog box with checkboxes

查看:110
本文介绍了实现与复选框对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的对话框

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;
         }
     }
 }

现在,我打开对话框,选中该复选框,单击按钮,关闭对话框。但是当我再次打开它,该复选框再次选中。我应该怎么办?

Now, I open the dialog and check the checkbox, click on button and dismiss the dialog. But When I open it again, the checkbox is again unchecked. what should i do?

推荐答案

不建议您使用对话框的方式了!你应该考虑使用的 DialogFragment

The way you are using your dialog is not recommended anymore! You should consider using a DialogFragment.

为什么你失去你的选中状态的问题,是因为当你再次打开对话框被重新创建。

The problem why you are loosing your checked state is because the dialog is recreated when you open it again.

查看DialogFragment方法的这个例子 http://developer.android.com /reference/android/app/DialogFragment.html 。你会看到,你可以传递参数对话框。

See this example of the DialogFragment approach http://developer.android.com/reference/android/app/DialogFragment.html. You will see that you can pass arguments to the dialog.

有关的解决方案,我建议,当你关闭你传递选定值回主办活动的对话框...当对话框应reopenned你只是传递参数的对话框,使该对话框设置其默认选择。

For a solution I recommend that when you close the dialog you pass the selected value back to the hosting activity... when the dialog should be reopenned you just pass the arguments to the dialog so that the dialog sets its default selection.

编辑:


  1. 既然你想显示复选框我将和适应
    例如:$从C $ C
    http://developer.android.com/guide/topics/ui/dialogs html的#AddingAList
    对于复选框。它仍然是方便使用
    AlertDialog.Builder。

  1. Since you would like to display checkboxes I will take and adapt the example code from http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList for checkboxes. It's still convenient to use the AlertDialog.Builder.

裹在Builder成DialogFragment通过覆盖
onCreateDialog法。您可以通过选择传递项列表
束为布尔数组,然后使用是setMultiChoiceItems。

Wrap the Builder into a DialogFragment by overwriting the onCreateDialog-method. You can pass the list of selected items via the Bundle as boolean array and then use is for setMultiChoiceItems.

公共静态类CheckBoxAlertDialogFragment扩展DialogFragment {

public static class CheckBoxAlertDialogFragment extends DialogFragment {

public static CheckBoxAlertDialogFragment newInstance(boolean[] checkedItems) {
    CheckBoxAlertDialogFragment frag = new CheckBoxAlertDialogFragment();
    Bundle args = new Bundle();
    args.putBooleanArray("checkedItem", 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();
}

}

在你的活动,你应该有一个变量,例如命名checkedItems布尔类型[]的地方节省的复选框状态。您可以打开对话框,然后用

In your activity you should have a variable, e.g. named checkedItems, of type boolean[] somewhere which saves the checkbox states. You can open the dialog then with

无效的ShowDialog(){
    DialogFragment newFragment = CheckBoxAlertDialogFragment.newInstance(checkedItems);
    newFragment.show(getFragmentManager(),对话);
}

void showDialog() { DialogFragment newFragment = CheckBoxAlertDialogFragment.newInstance(checkedItems); newFragment.show(getFragmentManager(), "dialog"); }

这篇关于实现与复选框对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆