今天看到「Android事件處理」一節,根據示例中重寫onKeyDown()方法來攔截用戶單擊後退按鈕事件,我試着運用onKeyDown()方法屏蔽HOME鍵,但發現如下代碼並不能屏蔽HOME鍵: ide
@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);
} this
經過Search in internet,我發現重寫onAttachedToWindow()方法能夠實現屏蔽HOME鍵,具體代碼以下: 事件
@Override
public void onAttachedToWindow() {
// TODO Auto-generated method stub
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
} get
@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);
} event
但onAttachedToWindow()方法有何功能, this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);一句又是什麼意思我不甚理解,盼前輩幫忙解惑! 方法