警报对话框不会关闭

时间:2018-08-18 12:46:53

标签: android android-alertdialog

下面的代码段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()
            }
    }

2 个答案:

答案 0 :(得分:0)

您应该可以使用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)

答案 1 :(得分:0)

问题已解决。

在我们称为alert.show的地方初始化builder.create

`var alertDialog:AlertDialog? =空     重写fun onReceive(context:Context,arg1:Intent){         var dialogBu​​ilder = AlertDialog.Builder(context).setTitle(“网络错误!”)                 .setCancelable(false)                 .setMessage(“检查您的互联网连接...”)

<?php

if(isset($_POST['execute'])) {

echo "<label>Result :</label>";

$folder = "files/quote/";
$overlay = $folder."overlay.png";
$font_quote = "files/_font/"."Ubuntu-Medium.ttf";
$font_copyright = "files/_font/"."Ubuntu-Medium.ttf";
$filename = $folder.md5(rand(000,999)).".png";
$quote = @$_POST['quote'] ? $_POST['quote'] : 'YOUR QUOTE';
$copyright = @$_POST['copyright'] ? $_POST['copyright'] : 'Username';
$backgrond = @$_POST['background'];

if (!filter_var($backgrond, FILTER_VALIDATE_URL) === false) {
    $bg = $backgrond;
}else {    
    $bg = get_redirect_target('https://source.unsplash.com/640x640/?'.urlencode($backgrond));
}

$image = new PHPImage();
$image->setQuality(10);
$image->setDimensionsFromImage($overlay);
$image->draw($bg);
$image->draw($overlay, '50%', '75%');
$image->setFont($font_quote);
$image->setTextColor(array(255, 255, 255));
$image->setAlignVertical('center');
$image->setAlignHorizontal('center');
$image->textBox($quote, array(
    'fontSize' => 28,
    'x' => 130,
    'y' => 240,
    'width' => 380,
    'height' => 200,
    'debug' => false
    ));

$image->setFont($font_copyright);
$image->setTextColor(array(255, 255, 255)); 
$image->text('CopyRight © ', array(
    'fontSize' => 15,
    'x' => 0,
    'y' => 535,
    'width' => 640,
    'height' => 20,
    'debug' => false
    ));

$image->setFont($font_copyright);
$image->setTextColor(array(230, 209, 65)); 
$image->text($copyright, array(
    'fontSize' => 15,
    'x' => 100,
    'y' => 535,
    'width' => 640,
    'height' => 20,
    'debug' => false
    ));

$image->save($filename);


$imagebase64 = "data:image/png;base64,".base64_encode(file_get_contents($filename));
echo "<a href='".$imagebase64."' target='_blank' download='$filename'><img src='".$imagebase64."'/></a>";
unlink($filename);
}

?>

`
谢谢老手