如何获得单选警报对话框的选定项目? [英] How to get selected item of a singlechoice Alert Dialog?
问题描述
我有这段代码来显示带有单选(单选)选项的对话框.
I have this code to show a dialog with singlechoice(radio) options.
AlertDialog ad = new AlertDialog.Builder(this)
.setCancelable(false)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.choose_one)
.setSingleChoiceItems(seq, pos,null)
.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int whichButton)
{
// dialog dismissed
}
}).create();
如何获得已选择的选择?
How do I get the choice that has been selected?
推荐答案
我知道这是一篇老文章,但是我只是偶然发现,发现此解决方案似乎比这里发布的内容更为简单.
I know this is an old post, but i just came across it, and found that this solution seems at bit more simple that whats been posted here.
>
您可以这样做:
You can just do like this:
在对话框的肯定按钮上的onClick()
处理程序中,添加以下代码:
In your onClick()
handler on the dialog positive button, add the following code:
ListView lw = ((AlertDialog)dialog).getListView();
Object checkedItem = lw.getAdapter().getItem(lw.getCheckedItemPosition());
注意,如果您未选择任何选项,它将崩溃,您必须在此处添加对勾,然后才能使用if(lw.getCheckedItemCount() > 0)
Note that if you haven't selected any option it will crash you have to add a check here before getting the checkedItem
with if(lw.getCheckedItemCount() > 0)
这篇关于如何获得单选警报对话框的选定项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!