2013-07-25 104 views
1

实现对话框这里是我的对话框与复选框

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

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

+0

的提示是的onCreate(捆绑savedInstanceState)给出。您需要保存复选框值,然后将其传回以从savedInstanceState包中检索它们。 –

+0

现在不好使用Dialog。强烈建议使用DialogFragment方法。 showDialog()从API级别13开始被弃用。请参阅下面的答案。 – a11n

回答

6

不推荐使用对话方式!您应该考虑使用DialogFragment

问题为什么你失去了检查状态是因为重新打开时会重新创建对话框。

请参见DialogFragment方法的示例http://developer.android.com/reference/android/app/DialogFragment.html。你会看到你可以传递参数给对话框。

对于解决方案,我建议当关闭对话框时,将选定的值传递回主机活动...当对话框应该重新打开时,只需将参数传递给对话框,以便对话框设置其默认选择。

编辑:

  1. 既然你想显示一个复选框,我会采取和 http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList 的复选框适应 示例代码。使用AlertDialog.Builder的 仍然很方便。

  2. 将生成器封装到DialogFragment中,方法是覆盖 onCreateDialog方法。您可以通过 Bundle将所选项目的列表作为布尔数组传递,然后将其用于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(); 
    } 
    } 
    
  3. 在你的活动,你应该有一个变量,例如名为checkedItems,类型为布尔型[]某处可以保存复选框状态。您可以打开对话框,然后用:

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

对不起。我仍然无法得到它。你可以向我展示你通过复选框状态的代码。特别是我对android和对话框很陌生。这将是非常有益的 – VipulKumar

1

您的复选框将在您每次显示/创建对话框时取消选中,因为在其onCreate方法中您有yes.setChecked(false);。除了解除对话框之外,您应该在解除对话之前保存复选框的值,并在每次创建对话框时获取该值以重置复选框。你可以使用SharedPreferences来保存这个值,即使你的Activity被销毁,或者根据需要来回传递你的主要活动的值。

感谢@andd,我忘记了Dialog已被弃用,他认为您应该使用DialogFragment,在这种情况下SharedPreferences不是必需的。

+1

但我认为DialogFragment需要最小的API 11,但在我的项目min API是8 – VipulKumar

+0

您可以使用Android 3.0之前的设备的支持片段 – TronicZomB