AlertDialog中的ListView

bug*_*gzy 26 android

我在AlertDialog中使用ListView来显示项目列表.当用户点击其中一个项目时,我希望关闭对话框.我不会在对话框上有任何操作按钮.关于如何实现这一目标的任何想法?

Eri*_*ass 92

你应该可以这样做:

final CharSequence[] items = {"Foo", "Bar", "Baz"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Make your selection");
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
         // Do something with the selection
    }
});
AlertDialog alert = builder.create();
alert.show();
Run Code Online (Sandbox Code Playgroud)

页面包含不同类型对话框的其他示例.

  • 只需将dismiss()放在public void onClick()函数中就会为你解除对话框. (5认同)