如何在AlertDialog中设置单项选择项?
我无法在 AlertDialog 中设置单项选择列表或多项选择列表。
我尝试遵循这些示例,但只得到一个带有标题、“确定”和“取消”按钮的对话框,没有列表,也没有消息(我设置的!)。
这是代码:
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DELETE_CITY:
CharSequence[] array = {"Red", "Blue", "Yellow"};
return new AlertDialog.Builder(ShowPypData.this)
.setTitle(R.string.city_actions_delete_label)
.setMessage(R.string.city_actions_delete_select_label)
.setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
default:
return null;
}
}
奇怪的是,如果我注释 setSingleChoiceItems 部分,我可以在对话框上看到消息。
I haven't been able to set a Single Choice list, or a Multiple Choice List inside an AlertDialog.
I tried following the examples but I only get a Dialog with a Title, the Ok and Cancel buttons, and no list, and NO message (which I set!).
Here is the code:
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DELETE_CITY:
CharSequence[] array = {"Red", "Blue", "Yellow"};
return new AlertDialog.Builder(ShowPypData.this)
.setTitle(R.string.city_actions_delete_label)
.setMessage(R.string.city_actions_delete_select_label)
.setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
default:
return null;
}
}
The weird thing is that if I comment the setSingleChoiceItems part, I can see the message on the dialog.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎
Buttons
、Message
和Multiple choice items
是互斥的。尝试注释掉setMessage()
、setPositiveButton()
和setNegativeButton()
。我自己没检查过。Seems that
Buttons
,Message
andMultiple choice items
are mutually exclusive. Try to comment outsetMessage()
,setPositiveButton()
andsetNegativeButton()
. Didn't check it myself.这段代码对我有用
this code works for me