让ProgressDialog在setCancelable(false)时按返回键可dismiss
最近发现Android4.0系统中ProgressDialog设置为setCancelable(true)时,点击ProgressDialog以外的区域也可以让ProgressDialog dismiss掉。但有时我们不希望是这样的效果,于是将ProgressDialog设置setCancelable(false),再点击ProgressDialog以外的区域就不会出现前面所说的情况了,然而这样以来再按返回键时就不起作用了,只能等待程序执行到dissmiss处为止。其实,只要为ProgressDialog添加KeyListener来对返回键予以处理就可以解决这个问题了。
-
package cn.winfirm.examples.base;
-
-
import android.app.Activity;
-
import android.app.ProgressDialog;
-
import android.content.DialogInterface;
-
import android.content.DialogInterface.OnKeyListener;
-
import android.os.Bundle;
-
import android.view.KeyEvent;
-
import android.view.Window;
-
-
/**
-
* Activity的基类,带一个ProgressDialog,可双击取消其显示
-
*
-
* @author savant
-
*
-
*/
-
public class BaseActivity extends Activity {
-
-
private ProgressDialog progressDialog = null;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
requestWindowFeature(Window.FEATURE_NO_TITLE);
-
}
-
-
/**
-
* show loading progress dialog
-
*/
-
public void showDialog() {
-
if (null == progressDialog) {
-
progressDialog = ProgressDialog.show(BaseActivity.this, "", "正在加载,请稍候...");
-
progressDialog.setCancelable(false);
-
} else {
-
progressDialog.show();
-
}
-
progressDialog.setOnKeyListener(onKeyListener);
-
}
-
-
/**
-
* add a keylistener for progress dialog
-
*/
-
private OnKeyListener onKeyListener = new OnKeyListener() {
-
@Override
-
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
-
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
-
dismissDialog();
-
}
-
return false;
-
}
-
};
-
-
/**
-
* dismiss dialog
-
*/
-
public void dismissDialog() {
-
if (isFinishing()) {
-
return;
-
}
-
if (null != progressDialog && progressDialog.isShowing()) {
-
progressDialog.dismiss();
-
}
-
}
-
-
/**
-
* cancel progress dialog if nesseary
-
*/
-
@Override
-
public void onBackPressed() {
-
if (progressDialog != null && progressDialog.isShowing()) {
-
dismissDialog();
-
} else {
-
super.onBackPressed();
-
}
-
}
- }