【问题标题】:How to create an alert dialog with radio button on the side?如何在侧面创建带有单选按钮的警报对话框?
【发布时间】:2009-10-30 05:07:32
【问题描述】:

如何创建一个带有RadioButtonAlertDialog? 我可以使用 AlertDialog.Builder 创建一个带有 3 个选择字符串的 Dialog,但是如何创建一个侧面带有 RadioButton 的字符串(即只允许 1 个选择)?

谢谢。

【问题讨论】:

    标签: android


    【解决方案1】:

    我不确定您是否在使用 Material Design,或者您在 6 年后是否仍需要此答案,但对于任何可能正在寻找此答案的人,这里是答案

        new MaterialDialog.Builder(this)
                            .title(R.string.which_phone_number)
                            .items(contactPhonesArr)
                            .itemsCallbackSingleChoice(0, new MaterialDialog.ListCallbackSingleChoice() {
                                @Override
                                public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                        //Do really cool things here
                                        dialog.dismiss();
                                    }
                                    return true;
                                }
                            })
                            .positiveText("Choose")
                            .show();
    

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      AlertDialog levelDialog;
      // Strings to Show In Dialog with Radio Buttons
      final CharSequence[] items = {" Easy "," Medium "," Hard "," Very Hard "};
      // Creating and Building the Dialog 
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle("Select The Difficulty Level");
      builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int item) {
              // Your code
              levelDialog.dismiss();    
          }
      });
      levelDialog = builder.create();
      levelDialog.show();
      

      【讨论】:

        【解决方案3】:

        也许在AlertDialog.Builder 中使用setView()?很难说“侧面有单选按钮的那个”是什么意思。

        【讨论】:

        • 谢谢。你能告诉我如何使用 setView() 在 AlertDialog 中设置 2 行文本吗?其中第一行是标题,第二行是描述?
        • 这听起来好像首选项可能是您正在寻找的......kaloer.com/android-preferences
        • 是否可以将 ListPreference 配置为 2 行文本而不是 1 行?谢谢。
        猜你喜欢
        • 1970-01-01
        • 2019-11-12
        • 2018-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多