这里是我的空间,是我用来记录点滴的沃土...

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();
}
posted @ 2012-04-06 20:27  wsx2miao  Views(3818)  Comments(0Edit  收藏  举报
这里是我的空间,是我用来记录点滴的沃土...