如何在Android中的警告对话框中添加多选复选框?

3

我已经在Android应用程序中创建了名为“同步”的菜单。当我们点击“同步”时,会打开一个包含4个复选框的布局警报。我希望它们像这样运作,当我点击15分钟时,其他选项会自动取消选择。

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
    return true;
}       
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
        case R.id.menu_settings:
            alertDialog = new AlertDialog.Builder(HomePage.this).create(); //Read Update
            LayoutInflater adbInflater = this.getLayoutInflater();
            View checkboxLayout = adbInflater.inflate(R.layout.sync_layout, null);
            defaultchkbox = (CheckBox)checkboxLayout.findViewById(R.id.defaultchkbox);
            after15mint = (CheckBox)checkboxLayout.findViewById(R.id.after15mint);
            after30mint = (CheckBox)checkboxLayout.findViewById(R.id.after30mint);
            after45mint = (CheckBox)checkboxLayout.findViewById(R.id.after45mint);
            alertDialog.setView(checkboxLayout);
            alertDialog.setTitle("Synchronization");
            alertDialog.setMessage("Choose");

            alertDialog.setButton(Dialog.BUTTON_POSITIVE,"Save changes", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    // TODO Auto-generated method stub
                    boolean checkBoxResult = false; 
                        if(after15mint.isChecked())
                        {
                            Toast.makeText(getApplicationContext(), "15 Minute checked", Toast.LENGTH_LONG).show();
                            checkBoxResult = true;
                        }
                        else if(after30mint.isChecked())
                        {
                            Toast.makeText(getApplicationContext(), "30 Minute checked", Toast.LENGTH_LONG).show();
                            checkBoxResult = true;
                        }
                        else if(after45mint.isChecked())
                        {
                            Toast.makeText(getApplicationContext(), "45 Minute checked", Toast.LENGTH_LONG).show();
                            checkBoxResult = true;
                        }   
                        else{
                            Toast.makeText(getApplicationContext(),     "Default", Toast.LENGTH_LONG).show();
                        }
                }
            });
            alertDialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int which) 
                {
                    alertDialog.dismiss();
                }
            });
            alertDialog.show(); 
            return true;
default:
            return super.onOptionsItemSelected(item);
    }
}

但是我对警报框中复选框的工作原理有点困惑。建议将是巨大的帮助。谢谢。 :)enter image description here

2个回答

3
看起来您需要处理嵌套在RadioGroup中的单选按钮,这样将只允许您一次选择一个选项。
有关RadioGroup的更多信息,请参见: http://developer.android.com/reference/android/widget/RadioGroup.html 有关创建单选按钮的更多信息,请参见: http://developer.android.com/reference/android/widget/RadioButton.html 特别是对于您的代码,您需要像下面这样在R.layout.sync_layout中定义RadioGroup内的RadioButtons:
<RadioGroup
    android:id="@+id/syncGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/defualt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text= "Default" 
        android:checked="true" />

    <RadioButton
        android:id="@+id/minute15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text= "15 Minute" />


     <RadioButton
         android:id="@+id/minute30"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text= "30 Minute" />


      <RadioButton
          android:id="@+id/minute45"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text= "45 Minute" />



</RadioGroup>

我需要仅使用复选框来实现。 - Shweta
有什么特别的原因吗?你可以节省编写代码的时间,避免手动勾选和取消复选框所需的所有手动工作,并且即使你实现了这一点,也可能会出现错误/漏洞。单选按钮是Android框架提供的具体API,可实现此目的。 - user_CC
好的,谢谢。让我试试单选按钮。感谢您的帮助。 - Shweta

2
看看这个链接:this
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.pick_color);
           .setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
               // The 'which' argument contains the index position
               // of the selected item
           }
    });
    return builder.create();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接