2015-05-06 103 views
0

我想攔截硬件鍵盤上的鍵盤事件。例如,當用戶按下「a」時,我不希望它被寫入EditText中。因此,當按下並釋放「a」鍵時,我會返回true,但無論如何它都會寫入EditText。哪裏不對?Android:攔截onKeyDown()和onKeyUp()

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    switch (keyCode) { 
     case KeyEvent.KEYCODE_A: 
      //Here it logs correct, but the letter appears in the EditText anyway 
      Log.d("Textboard","A"); 
      return true; 

     default: 
      return super.onKeyDown(keyCode, event); 
    } 
} 

@Override 
public boolean onKeyUp(int keyCode, KeyEvent event) { 
    switch (keyCode) { 
     case KeyEvent.KEYCODE_A: 
      //Here it logs correct, but the letter appears in the EditText anyway 
      Log.d("Textboard","A"); 
      return true; 

     default: 
      return super.onKeyUp(keyCode, event); 
    } 
} 

回答

0

活動的onKeyXXX()比EditText的onKeyXXX()晚。

如果要截取它,只需覆蓋dispatchKeyEvent()即可。