获取 AlertDialog 选中的 Item

Get AlertDialog selected Item

如何从 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();

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

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();

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