在android中如何在收到实时消息时关闭对话框?

时间:2015-10-27 13:27:44

标签: android

单击按钮时会出现一个对话框。代码如下。

public void howtoplayvideo(View v){

final Dialog dialog = new    Dialog(this,android.R.style.Theme_DeviceDefault_NoActionBar_Fullscreen);
    dialog.setContentView(R.layout.dialogbox);


    Button exit = (Button) dialog.findViewById(R.id.done);
    exit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog_open=false;    //flag to know when dialog is open
            dialog.dismiss();
        }
    });
    dialog_open=true;
    dialog.show();
}

当查看此对话框时,如果收到实时消息,那么如何从onRealTimeMessageReceived关闭此对话框

@Override
public void onRealTimeMessageReceived(RealTimeMessage rtm) {
    byte[] buf = rtm.getMessageData();
int button = (int) buf[0];
switch (button) {
        case 1:
              if(dialog_open) {
              //how to access and close dialog box here
             }
         }}

如果我在方法之外声明了dialogbox public,则会出现在onCreate之前无法初始化的错误。

3 个答案:

答案 0 :(得分:0)

Dialog mDialog; 
public void howtoplayvideo(){

        mDialog = new    Dialog(this,android.R.style.Theme_DeviceDefault_NoActionBar_Fullscreen);
        mDialog.setContentView(R.layout.dialogbox);


        Button exit = (Button) mDialog.findViewById(R.id.done);
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });

        mDialog.show();
    }

@Override
public void onRealTimeMessageReceived(RealTimeMessage rtm) {
    byte[] buf = rtm.getMessageData();
    int button = (int) buf[0];

     switch (button) {
        case 1:
               if (mDialog != null && mDialog.isShowing())
                    mDialog.dismiss();
              break;
         }}

将对话框作为类变量,然后可以在此类的其他方法中使用此对话框。

不需要为检查天气对话框显示或不显示额外的布尔值, 为此使用dialog.isShowing()。

答案 1 :(得分:0)

下面的代码可以解决问题。

无需保持对话框是否显示的布尔值。您始终可以使用dialog.isShowing()函数来检查它。

private Dialog dialog;

public void howtoplayvideo(View v){

    dialog = new Dialog(this, android.R.style.Theme_DeviceDefault_NoActionBar_Fullscreen);
    dialog.setContentView(R.layout.dialogbox);

    Button exit = (Button) dialog.findViewById(R.id.done);
    exit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();
}

@Override
public void onRealTimeMessageReceived(RealTimeMessage rtm) {
    byte[] buf = rtm.getMessageData();
    int button = (int) buf[0];
    switch (button) {
        case 1:
            if(dialog != null) {
                dialog.dismiss();
            }
        }
}

答案 2 :(得分:0)

在全球范围内声明Dialog,并且不需要boolean值...您可以使用isShowing()这样的方法...并关闭对话框..

@Override
public void onRealTimeMessageReceived(RealTimeMessage rtm) {
    byte[] buf = rtm.getMessageData();
int button = (int) buf[0];
switch (button) {
        case 1:
                if(dialog.isShowing()){
                         dialog.dismiss();
                     }
                    break;
             }
}