Android 如何获取单选警报对话框的选定项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2494171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:04:15  来源:igfitidea点击:

How to get selected item of a singlechoice Alert Dialog?

androidandroid-alertdialog

提问by Pentium10

I have this code to show a dialog with singlechoice(radio) options.

我有这个代码来显示一个带有 singlechoice(radio) 选项的对话框。

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?

我如何获得已选择的选项?

回答by Dan

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:

你可以这样做:

In your onClick()handler on the dialog positive button, add the following code:

onClick()对话框肯定按钮上的处理程序中,添加以下代码:

ListView lw = ((AlertDialog)dialog).getListView();
Object checkedItem = lw.getAdapter().getItem(lw.getCheckedItemPosition());

Notethat if you haven't selected any option it will crash you have to add a check here before getting the checkedItemwith if(lw.getCheckedItemCount() > 0)

请注意,如果您没有选择任何选项,它会崩溃,您必须在获取checkedItemwith之前在此处添加检查if(lw.getCheckedItemCount() > 0)

回答by Matt Brian?on

I tried to use ListView.setSelection(int)but it never worked as expected so instead I decided to make use of View.setTag()to temporarily store the selected position.

我尝试使用ListView.setSelection(int)但它从未按预期工作,因此我决定使用View.setTag()来临时存储所选位置。

.setSingleChoiceItems(adapter, -1,
        new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            ListView lv = ((AlertDialog)dialog).getListView();
            lv.setTag(new Integer(which));
        }
})

The tag can then be accessed easily after a button click.

单击按钮后,可以轻松访问该标签。

.setPositiveButton(R.string.button_text,
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        ListView lv = ((AlertDialog)dialog).getListView();
        Integer selected = (Integer)lv.getTag();
        if(selected != null) {
            // do something interesting
        }
    }
})

回答by Dan Lew

I believe that you use an OnClickListenerfor setSingleChoiceItems(), to listen whenever an item has been selected; then once the user hits okay, you set that item in stone. Right now you're just passing null, so nothing you can't pick up which item was selected.

我相信您使用的OnClickListenersetSingleChoiceItems(),只要选择物品就要倾听; 然后一旦用户点击OK,你就将该项目固定在石头上。现在你只是传递null,所以没有什么你不能选择哪个项目被选中。

回答by Bishan

1). create Array.

1)。创建数组。

final ArrayList<String> arrData = new ArrayList<String>();

2). add data to array you have created.

2)。将数据添加到您创建的数组中。

if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {

                    arrData .add(cursor.getString(1)); 
                                             // (1 = int columnIndex)

                } while (cursor.moveToNext());
            }
        }

3). get data like this.

3)。得到这样的数据。

public void onClick(DialogInterface dialog, int item) {

                        Log.d("Selected", arrData.get(item) + "");

                    }

4). that's all :)

4)。就这样 :)

回答by patel135

You can do just like this in on onClick()method

你可以在onClick()方法中这样做

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.edit_set_waiting_period)
                .setItems(R.array.str_set_waiting_period, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // The 'which' argument contains the index position
                        // of the selected item
                        L.e("selectedItmes", which + "");

                        ListView lw = ((AlertDialog) dialog).getListView();
                        Object checkedItem = lw.getAdapter().getItem(which);
                        L.e("checkedItem", checkedItem.toString() + "");

                    }
                });

        builder.show();

回答by priojeet priyom

position of the selected element is already given in the onClick method. so we can directly access the the array and get the selected item without any hassle. in this case: seq[which] will be the selected item.

所选元素的位置已经在 onClick 方法中给出。所以我们可以直接访问数组并轻松获取所选项目。在这种情况下: seq[which] 将是所选项目。

回答by Dinesh

EditText et = (EditText)findViewById(R.id.editText9);

int a = Integer.valueOf(item);

et.setText(items[a]);