在點擊editText之類的物件跳出鍵盤, 輸入資料後若無法關閉

則可取用以下程式碼, 即可關閉虛擬鍵盤

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);

@Override
     public boolean onTouchEvent(MotionEvent event) {
      // TODO Auto-generated method stub
      if(event.getAction() == MotionEvent.ACTION_DOWN){
         if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
           imm.hideSoftInputFromWindow
           (getCurrentFocus().getWindowTokeInputMethodManager.HIDE_NOT_ALWAYS);
         }
      }
      return super.onTouchEvent(event);
     }

或者將下列這行程式碼寫入監聽事件內, 也可關閉虛擬鍵盤

imm.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
arrow
arrow
    文章標籤
    android 關閉虛擬鍵盤
    全站熱搜

    Will 發表在 痞客邦 留言(0) 人氣()