Android开发:TabActivity中onKeyDown无法响应的解决方法
Android中某个类继承Activity的子类TabActivity时,重载Activity中的public boolean onKeyDown(int keyCode, KeyEvent event)方法时,点击按键时并得不到响应,解决的方法是:
重载public boolean dispatchKeyEvent(KeyEvent event)方法,其示例代码如下:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
this.showTips();//这是自定义的代码
}
return true;
}
return super.dispatchKeyEvent(event);
}
private void showTips() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("退出后,要重新登录,确定退出吗?");
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
return;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.create().show();
}