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();
}
*/

posted @ 2022-12-26 10:43  阿风小子  阅读(320)  评论(0编辑  收藏  举报