如何查看Alert Dialog可见性?

时间:2017-04-27 21:56:04

标签: java android

我想检查警报对话框是否可见。在大多数帖子中,我看到他们使用isShowing,但现在似乎无法描述。

当用户点击信息textview时,我会暂停音乐。如果用户关闭警报对话框,音乐将再次播放。

{{1}}

3 个答案:

答案 0 :(得分:1)

您可能想要将一个OnDismissListener添加到playstopbutton_builder:

        playstopbutton_builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            mediaControl.start();
        }
    });

这样,当用户取消提示对话框时,音乐将再次开始播放。

编辑:如果不需要OnDismissListener方法,也许这样的事情会更好:

public void infoClickHandler(View v) {
    mediaControl.pause();

    AlertDialog.Builder b = new AlertDialog.Builder(this);
    b.setMessage("restart the music?");
    b.setPositiveButton("ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            mediaControl.start();
        }
    });
    b.show();
}

编辑2:另一方面,如果对话框没有正面或负面按钮,并且你不想将cancellable设置为false,那么这似乎有效:

public void infoClickHandler(View v) {
    mediaControl.pause();

    AlertDialog.Builder b = new AlertDialog.Builder(this);
    b.setMessage("restart the music?");
    b.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            if (!mediaControl.isPlaying()) {
                mediaControl.start();
            }
        }
    });
    b.show();
}

当用户点击对话框外部时,将调用OnDismissListener。

答案 1 :(得分:1)

由于您已将Cancelable设为false,因此您可能需要使用

正面按钮说好的

playstopbutton_builder.setPositiveButton(positiveBtnText,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    //resume ur media player here
                }
            })

对于否定按钮说像取消

playstopbutton_builder.setNegativeButton(negativeBtnText,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    //resume ur media player here
                }
            })

所以看起来像这样

info_Button.setClickable(true);
info_Button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        mediaControl.pause();
        AlertDialog.Builder playstopbutton_builder = new AlertDialog.Builder(exercise_arm_triceps_execute.this);
        playstopbutton_builder.setTitle("WARNING").setMessage("Please get warm before exercising!");
        playstopbutton_builder.create().show();
        playstopbutton_builder.setCancelable(false);
playstopbutton_builder.setPositiveButton(positiveBtnText,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    //resume ur media player here
                }
            });
playstopbutton_builder.setNegativeButton(negativeBtnText,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    //resume ur media player here
                }
            });
    }
});

答案 2 :(得分:1)

您需要检查对话框显示或不更改您的代码。

info_Button.setClickable(true); 
    info_Button.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) {
            mediaControl.pause(); 
            AlertDialog.Builder playstopbutton_builder = new AlertDialog.Builder(exercise_arm_triceps_execute.this);
            playstopbutton_builder.setTitle("WARNING").setMessage("Please get warm before exercising!");
            playstopbutton_builder.create();
            playstopbutton_builder.setCancelable(false);

            //if alert dialog is visible keep music paused 
            //else if mediaControl.start(); 

            if(!playstopbutton_builder.isShowing()){ 

  //if its visibility is not showing then show here 

                  playstopbutton_builder.show();  

             }else{ 

  //do something here... if already showing 

             } 

        } 
    });