单击 PositiveButton 后,Android 不要关闭 AlertDialog

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26086848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:59:11  来源:igfitidea点击:

Android Don't dismiss AlertDialog after clicking PositiveButton

androidandroid-alertdialog

提问by Yi-Ying Lu

Can I just don't dismiss my AlertDialog after clicking PositiveButton?

单击 PositiveButton 后,我可以不关闭 AlertDialog 吗?

I would like to remain the dialog to show something update on my ArrayAdapter listWords.

我想保留对话框以在我的 ArrayAdapter listWords 上显示一些更新。

This is my code.

这是我的代码。

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

回答by Chitrang

After looking at @Little Child solution, I try to make this. Let us know if this works for you.

在查看@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();

回答by Little Child

based on the most voted answer for How to prevent a dialog from closing when a button is clicked

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

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

                }
            });
        }
    });  

I believe you need to override the positive button's handler. Add your logic to dismiss the dialog when a certain condition is met.

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

回答by M. Usman Khan

Even Simpler:

更简单:

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

回答by Steve Lukis

Answer in Kotlin :

在 Kotlin 中回答:

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
        }

    }

回答by Yury Matatov

I do it like this:

我这样做:

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

        }
    });