android-如何防止弹出窗口被窗口单击关闭? [英] How to prevent popup from closing by outside the window click in android?

查看:368
本文介绍了android-如何防止弹出窗口被窗口单击关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动中,我将打开一个弹出窗口(警报对话框),其中包含yes和no选项.但是,当我在窗口外部或屏幕上的任何位置单击时,弹出窗口将被关闭.如何通过单击任意位置来防止弹出窗口关闭.我希望当我仅输入否"按钮时,它应该关闭.请帮忙!!!

In my activity i'm opening a popup (alert dialog) with yes and no option. But when I click outside the window or anywhere in the screen the popup is dismissed. How can I prevent the popup from closing by clicking anywhere. I want that it should close when I enter No button only. Please help!!!

这是MainActivity.java中的弹出代码

Here is the popup code in MainActivity.java

Handler handler = new Handler();

Handler handler = new Handler();

handler.postDelayed(new Runnable() {

    @Override

    public void run() {


   if (context != null) {

            AlertDialog.Builder alert = new AlertDialog.Builder(context, R.style.MyAlertDialogStyle)
                    .setTitle("Title")
                    .setMessage("Message")
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // continue with delete
                            Intent intent = new Intent(MainActivity.this, WebActivity.class);
                            startActivity(intent);
                        }
                    })
                    .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int which) {
                            dialogInterface.dismiss();
                            // do nothing
                        }
                    });

            mDialog = alert.create();
            mDialog.getWindow().getAttributes().windowAnimations = R.style.MyAlertDialogStyle;
            if (!((Activity) context).isFinishing())
            mDialog.show();
            // .setIcon(R.drawable.inr1)
            // .show();

        }
    }
}, 15000);

推荐答案

只需添加以下行即可解决您的问题

simply adding following line will solve your problem

mDialog .setCanceledOnTouchOutside(false);

这篇关于android-如何防止弹出窗口被窗口单击关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆