2019独角兽企业重金招聘Python工程师标准>>>
今天看到“Android事件处理”一节,根据示例中重写onKeyDown()方法来拦截用户单击后退按钮事件,我试着运用onKeyDown()方法屏蔽HOME键,但发现以下代码并不能屏蔽HOME键:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_HOME){
return true;
}
return super.onKeyDown(keyCode, event);
}
通过Search in internet,我发现重写onAttachedToWindow()方法可以实现屏蔽HOME键,具体代码如下:
@Override
public void onAttachedToWindow() {
// TODO Auto-generated method stub
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_HOME){
return true;
}
return super.onKeyDown(keyCode, event);
}
但onAttachedToWindow()方法有何功能, this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);一句又是什么意思我不甚理解,盼前辈帮忙解惑!