首页 > 其他分享 >android处理Back键Home键和Menu键事件

android处理Back键Home键和Menu键事件

时间:2022-12-26 10:59:20浏览次数:46  
标签:false Menu builder Back return dialog Home android keyCode

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

标签:false,Menu,builder,Back,return,dialog,Home,android,keyCode
From: https://www.cnblogs.com/kn-zheng/p/17005203.html

相关文章