Android单击PositiveButton后不要关闭AlertDialog

Yi-*_* Lu 42 android android-alertdialog

点击PositiveButton后我可以不解雇我的AlertDialog吗?

我想继续在对话框中显示我的ArrayAdapter listWords上的更新内容.

这是我的代码.

AlertDialog.Builder sayWindows = new AlertDialog.Builder(MapActivity.this);

final EditText saySomething = new EditText(MapActivity.this);

sayWindows.setPositiveButton("ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    say = userName + " Says: "+saySomething.getText();
                    showPosition.setText(say);                      
                }
            });

sayWindows.setNegativeButton("cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

sayWindows.setAdapter(listWords, null);
sayWindows.setView(saySomething);
sayWindows.create().show();
Run Code Online (Sandbox Code Playgroud)

Chi*_*ang 65

在看了@Little Child解决方案之后,我试着做到这一点.如果这对您有用,请告诉我们.

    AlertDialog.Builder sayWindows = new AlertDialog.Builder(
            MapActivity.this);
    final EditText saySomething = new EditText(MapActivity.this);
    sayWindows.setPositiveButton("ok", null);
    sayWindows.setNegativeButton("cancel", null);
    sayWindows.setAdapter(listWords, null);
    sayWindows.setView(saySomething);

    final AlertDialog mAlertDialog = sayWindows.create();
    mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {

            Button b = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Do something
                   say = userName + " Says: "+saySomething.getText();
                   showPosition.setText(say); 
                }
            });
        }
    });
    mAlertDialog.show();
Run Code Online (Sandbox Code Playgroud)


Lit*_*ild 13

基于如何在单击按钮时阻止对话框关闭的最多投票答案

final AlertDialog d = new AlertDialog.Builder(context)
            .setView(v)
            .setTitle(R.string.my_title)
            .setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
            .setNegativeButton(android.R.string.cancel, null)
            .create();

    d.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {

            Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Do something

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

我相信你需要覆盖正面按钮的处理程序.添加逻辑以在满足特定条件时关闭对话框.


M. *_*han 10

更简单:

final AlertDialog alertDialog = new AlertDialog.Builder(context).setView(v)
                .setPositiveButton(android.R.string.ok, null)
                .setNegativeButton(android.R.string.cancel, null)
                .show();

        Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //Do Your thing
            }
        });
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的答案 (2认同)

小智 5

用科特林回答:

val dialog = AlertDialog.Builder(context)
    .setView(v)
    .setTitle(R.string.my_title)
    .setPositiveButton(android.R.string.ok, null)
    .setNegativeButton(android.R.string.cancel, null)
    .create()

dialog.setOnShowListener {

        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
            // Apply logic here
        }

    }
Run Code Online (Sandbox Code Playgroud)


Yur*_*tov 5

我这样做是这样的:

    final AlertDialog dialog = new AlertDialog.Builder(this)
            .setCancelable(false)
            .setPositiveButton("YES", null)
            .setNegativeButton("NO", null)
            .show();

    Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    positiveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
       //     Toast.makeText(SysManagerActivity.this, "dialog is open", Toast.LENGTH_SHORT).show();

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