取消Android中Activity的onPause方法中的AlertDialog

时间:2014-11-21 06:32:15

标签: android android-alertdialog cancel-button

以下是我的AlertDialog代码,

AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("GPS settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,int which) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        mContext.startActivity(intent);
       }
 });

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
       dialog.cancel();
       }
   });
alertDialog.show();

@Override
protected void onPause() {
    super.onPause();
 // I want dialog to be dismissed here

}

它的工作正常,但我想要的是在Activity的onPause方法中解除对话框,
我怎么能实现它?

3 个答案:

答案 0 :(得分:1)

onPause

中使用此功能
alertDialogforGPS.dismiss();

修改

AlertDialog.Builder builder;

builder = new AlertDialog.Builder(WelcomeActivity.this).setTitle("Title")
.setMessage("Message")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int which) {
        //your code
    }
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});
AlertDialog dialog = builder.create();
dialog.show();

并在onPause

dialog.dismiss();

答案 1 :(得分:0)

试试这个..

声明全局变量..

AlertDialog.Builder alertDialog;

在需要时定义并访问它。

alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("GPS settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,int which) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
       dialog.dismiss(); // NOTE : dismiss dialog before moving to other activity.. it will prevent causing exception... 
        mContext.startActivity(intent);
       }
 });

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
       dialog.dismiss();
       }
   });

以其他方法访问变量..

@Override
protected void onPause(){
    //checking for null variable
    if(alertDialog != null)
        alertDialog.dismiss();
    }
}

答案 2 :(得分:0)

首先,您必须让AlertDialog.Builder alertDialog成为您班级的成员。像:

private AlertDialog.Builder mAlertDialog;

然后,在您定义警报对话框的位置,使用此成员:

mAlertDialog = new AlertDialog.Builder(mContext);
mAlertDialog.setTitle("GPS settings");
mAlertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
mAlertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int which) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        mContext.startActivity(intent);
    }
});

mAlertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});
mAlertDialog.show();

然后在onPause()方法中,您可以关闭对话框。

@Override
protected void onPause() {
    if (mAlertDialog != null) {
        mAlertDialog.dismiss();
    }
    super.onPause();
}