【问题标题】:How to prevent popup from closing by outside the window click in android?如何防止弹出窗口在android中单击窗口外关闭?
【发布时间】:2015-12-14 10:49:26
【问题描述】:

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

这是 MainActivity.java 中的弹出代码

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

【问题讨论】:

  • 也许 .setCancelable(false) 到 AlertDialog.Builder?
  • @RaviRupareliya 在哪里使用这个命令?在 Handler 之外或就在 mDialog.show() 的下方; ???

标签: android android-fragments android-intent android-activity popup


【解决方案1】:

mDialog.show();之前添加这一行

mDialog .setCanceledOnTouchOutside(false);

【讨论】:

    【解决方案2】:

    只需添加

    mDialog.setCancelable(false);
    

    【讨论】:

      【解决方案3】:

      试试mDialog.setCancelable(true);

      【讨论】:

      • 或 mDialog.setCanceledOnTouchOutside(false);
      【解决方案4】:

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

      mDialog .setCanceledOnTouchOutside(false);
      

      【讨论】:

        猜你喜欢
        • 2023-02-09
        • 2018-05-24
        • 1970-01-01
        • 1970-01-01
        • 2012-12-21
        • 1970-01-01
        • 1970-01-01
        • 2012-08-28
        • 1970-01-01
        相关资源
        最近更新 更多