【问题标题】:Alert Dialog does not dismiss警报对话框不会关闭
【发布时间】:2019-12-05 16:13:40
【问题描述】:

在下面的代码中 sn-p alertdialog.dismiss 不工作不知道为什么。日志工作正常,但对话框不会关闭。 覆盖 fun onReceive(context: Context, arg1: Intent) {

        var builder = AlertDialog.Builder(context)
                .setTitle("Network Error !")
                .setMessage("Check your internet connection...")
                .setCancelable(false)
        var alertDialog:AlertDialog = builder.create()

            if (isConnectedOrConnecting(context)) {
                    alertDialog.dismiss()
                    Log.i("Network","Alive")

            } else{
                Log.i("Network","Dead")
                alertDialog.show()
                //alertDialog.dismiss()
            }
    }

【问题讨论】:

  • 您的警报对话框不会从屏幕上消失吗@venkatachalam S
  • 从您的日志中,“network”的值是多少?是“死”还是“活”?
  • @Taslim 我在广播接收器中编写了代码。根据我的网络连接,我得到的网络价值是活的和死的。但警报对话框没有被关闭
  • @Pie 是的。我的警报对话框没有被解雇,但活着的价值日志没有任何问题
  • @venkatachalam 你看到我的回答了吗。

标签: android android-alertdialog


【解决方案1】:

问题已解决。

在我们调用alert.show的地方初始化builder.create

var alertDialog:AlertDialog? = null
override fun onReceive(context: Context, arg1: Intent) {
    var dialogBuilder = AlertDialog.Builder(context).setTitle("Network Error !")
            .setCancelable(false)
            .setMessage("Check your internet connection...")

    if (isConnectedOrConnecting(context)) {
        //initializeDialog(context)
        alertDialog!!.dismiss()
        Log.i("Network","Alive")
    }else{
        alertDialog = dialogBuilder.create()
        alertDialog!!.show()
        Log.i("Network","Dead")
        //initializeDialog(context).create()
    }
}

【讨论】:

    【解决方案2】:

    您应该可以使用 Dialog.dismiss() 或 Dialog.cancel()

    alertDialog.setNeutralButton("OK", new DialogInterface.OnClickListener() { // define the 'Cancel' button
        public void onClick(DialogInterface dialog, int which) {
            //Either of the following two lines should work.
            dialog.cancel();
            //dialog.dismiss();
        } 
    });
    

    否则您可以在几秒钟后关闭对话框

    final AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Leaving launcher").setMessage("Are you sure you want to leave the launcher?");
    dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int whichButton) {
            exitLauncher();
        }
    });     
    final AlertDialog alert = dialog.create();
    alert.show();
    
    // Hide after some seconds
    final Handler handler  = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (alert.isShowing()) {
                alert.dismiss();
            }
        }
    };
    
    alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            handler.removeCallbacks(runnable);
        }
    });
    
    handler.postDelayed(runnable, 10000)
    

    【讨论】:

    • 我不想要按钮。我需要一个没有按钮的警报对话框。当互联网连接不可用时对话框显示,但当网络连接可用时,我想关闭警报对话框。
    • @venkatachalam 如果互联网不可用,您可以在构建器中设置设置确定按钮。用户点击它,它将从屏幕上消失
    • 你看到我的回答了吗?我已经更新了我的答案。 @venkatachalam
    猜你喜欢
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多