【发布时间】:2009-10-30 05:07:32
【问题描述】:
如何创建一个带有RadioButton 的AlertDialog?
我可以使用 AlertDialog.Builder 创建一个带有 3 个选择字符串的 Dialog,但是如何创建一个侧面带有 RadioButton 的字符串(即只允许 1 个选择)?
谢谢。
【问题讨论】:
标签: android
如何创建一个带有RadioButton 的AlertDialog?
我可以使用 AlertDialog.Builder 创建一个带有 3 个选择字符串的 Dialog,但是如何创建一个侧面带有 RadioButton 的字符串(即只允许 1 个选择)?
谢谢。
【问题讨论】:
标签: android
我不确定您是否在使用 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();
【讨论】:
你可以试试这个:
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();
【讨论】:
也许在AlertDialog.Builder 中使用setView()?很难说“侧面有单选按钮的那个”是什么意思。
【讨论】: