[Android]更改dialog返回監聽

Share on FacebookShare on Google+Tweet about this on TwitterShare on Tumblr

更改dialog返回監聽
程式碼:

private OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
		public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
			if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
				return true;
			} else {
				return false;
			}
		}
	};

在創建的時候添加

AlertDialog dlg = new AlertDialog.Builder(this).create();
dlg.setOnKeyListener(keylistener);
dlg.setCancelable(false);
dlg.show();