【问题标题】:How to Add multiple choice checkboxes in alert Dialog in Android?如何在 Android 的警报对话框中添加多项选择复选框?
【发布时间】:2013-04-10 01:15:05
【问题描述】:

我在安卓应用中创建了菜单“同步”。当我们单击“同步”警报时,会打开一个 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);
    }
}

但我对警报中复选框的工作有点困惑。建议会有很大帮助。谢谢你。 :)

【问题讨论】:

    标签: java android radio-button checkbox android-alertdialog


    【解决方案1】:

    看起来您需要使用嵌套在 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>
    

    【讨论】:

    • 我只需要使用复选框来实现
    • 有什么特别的原因吗?您将节省您必须正确执行所有手动工作以检查和取消选中复选框的代码,即使您实现了这一点,也可能容易出现错误/错误。 Radio Buttons 是 Android 框架提供的一个具体的 API 来实现这样的目的
    • 好的,谢谢。让我试试单选按钮。感谢您的帮助。
    【解决方案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();
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-25
      • 2012-08-29
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 2013-06-26
      • 2020-11-14
      相关资源
      最近更新 更多