티스토리 뷰

Activity를 상속 한 상태에서

@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { // 백 버튼
  Toast.makeText(this, "Back키를 누르셨군요, Toast.LENGTH_SHORT).show();
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_SEARCH) { // 검색버튼
  Toast.makeText(this, "검색키를 누르셨군요, Toast.LENGTH_SHORT).show();
        }
        return true;
    }

또는

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
     // TODO Auto-generated method stub

     switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
  Toast.makeText(this, "Back키를 누르셨군요, Toast.LENGTH_SHORT).show();
   break;
}

댓글