是否可以在AlertDialog上创建一个不会自动关闭对话框的按钮?

Par*_*ker 3 android android-alertdialog

我有一个简单的列表视图,在警告对话框中有一些复选框.我需要选择添加select all/none,但是你不能在警告对话框中显示菜单,我想从一个按钮执行此功能.从我所看到的任何类型的按钮(正面,中性和负面)都关闭对话框,无论如何.

那么,这可能吗?如果不是,我有什么替代品?我的最后一个缓解是简单地创建一个新视图并重新创建所有内容.新视图是最佳解决方案吗?

Com*_*are 6

那么,这可能吗?

不是来自AlertDialogAFAIK 的标准按钮.

如果不是,我有什么替代品?

这里有一些:

  1. 不要提供全选/无
  2. 不要使用AlertDialog,而是使用以对话为主题的活动
  3. 不要使用AlertDialog,但使用其他一些子类Dialog
  4. 您引用的选项(不要使用setAdapter(),而是View为对话框内容创建自己的选项)
  5. 根本不要使用a Dialog,而是ListActivity启动通过startActivityForResult()(类似于#2,不用担心主题)


m4n*_*k4s 5

您必须覆盖按钮的onClickListener.例如,对于中性按钮,您将具有以下内容:

AlertDialog dialog = builder.create();
dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Neutral", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    } 
});
dialog.setButton(AlertDialog.BUTTON_POSITIVE, "Positive", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    } 
});
dialog.show();

Button neutralButton = dialog.getButton(DialogInterface.BUTTON_NEUTRAL);
neutralButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View onClick) {                 
        /**
        *
        * your code
        * 
        */

    }
});
Run Code Online (Sandbox Code Playgroud)