SSISO Community

시소당

[Android] BackKey (취소키) 에 액션걸기

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;
}



출처: http://devbible.tistory.com/54 [devbible]

384 view

4.0 stars