获取AlertDialog选择的项目

时间:2015-02-06 11:49:27

标签: android android-alertdialog

如何从AlertDialog

获取所选项目文本
AlertDialog.Builder b = new AlertDialog.Builder(SettingsCustom.this);
b.setTitle("Example");
String[] types = {"Item 1","Item 2","Item 3" };
b.setItems(types, new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialogInterface, int which) {
        dialogInterface.dismiss();

        //How to get the selected text
    }
});

b.show();

2 个答案:

答案 0 :(得分:0)

我找到了办法。但不知道这是不是最好的方式。

AlertDialog.Builder b = new AlertDialog.Builder(SettingsCustom.this);
b.setTitle("Example");
final String[] types = {"Item 1","Item 2","Item 3" };
b.setItems(types, new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialogInterface, int which) {
        dialogInterface.dismiss();

        //How to get the selected text
        Log.v("-----> " , "selected text = " + types[which]);
    }
});

b.show();

答案 1 :(得分:0)

您需要注册DialogInterface.OnClickListener并使用setPostivie,setsinglechoice,setmultichoice等方法。