更新:私はこれについて興味がありました。android.view.Viewソースコードを見てください:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/view/View。 java
典型的な例は、IMEがそれを認識してそれ自体を閉じるのではなく、BACKキーを処理してアプリケーションのUIを更新することです。
コード:
/**
* Handle a key event before it is processed by any input method
* associated with the view hierarchy. This can be used to intercept
* key events in special situations before the IME consumes them; a
* typical example would be handling the BACK key to update the application's
* UI instead of allowing the IME to see it and close itself.
*
* @param keyCode The value in event.getKeyCode().
* @param event Description of the key event.
* @return If you handled the event, return true. If you want to allow the
* event to be handled by the next receiver, return false.
*/
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
return false;
}
dispatchKeyEventの使用:
@Override
public boolean dispatchKeyEvent (KeyEvent event) {
Log.d("**dispatchKeyEvent**", Integer.toString(event.getAction()));
Log.d("**dispatchKeyEvent**", Integer.toString(event.getKeyCode()));
if (event.getAction()==KeyEvent.ACTION_DOWN && event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
Toast.makeText(this, "Back button pressed", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
バックキーの場合でも、2つのイベントを個別にログに記録します。ログに記録されなかった唯一のキーはKEYCODE_HOME
、何らかの理由ででした。実際、戻るボタンを押したままにすると、いくつかのACTION_DOWN
(0)イベントが連続して表示されます(return false;
代わりに、さらに多くのイベントが表示されます)。EclairエミュレーターとSamsungCaptivate(カスタムFroyo ROM)でテストされています。