android处理Back键Home键和Menu键事件(转)

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{ //监控/拦截/屏蔽返回键
dialog(); return false; } else if(keyCode == KeyEvent.KEYCODE_MENU)
{
if(isShowButton)
{
rly.setVisibility(View.VISIBLE);
isShowButton = false; }else
{
rly.setVisibility(View.GONE);
isShowButton = true;
} return false; } else if(keyCode == KeyEvent.KEYCODE_HOME)
{
//由于Home键为系统键,此处不能捕获,需要重写onAttachedToWindow() return false;
}
return super.onKeyDown(keyCode, event);
} protected void dialog()
{
AlertDialog.Builder builder = new Builder(WebActivity.this);
builder.setMessage("确定要退出吗?");
builder.setTitle("提示");
builder.setPositiveButton("确认",
new android.content.DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
WebActivity.this.finish();
}
}); builder.setNegativeButton("取消",
new android.content.DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
}); builder.create().show();
} /*
// 拦截/屏蔽系统Home键
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
*/
上一篇:perl学习之路3


下一篇:android屏蔽BACK键、HOME键和多任务键