1. 程式人生 > >Android彈出軟鍵盤和關閉軟鍵盤

Android彈出軟鍵盤和關閉軟鍵盤

在一些有輸入框的介面中,我們需要自動彈出軟鍵盤。比如搜尋介面,輸入框需要自動獲取焦點並彈出軟鍵盤。

彈出軟鍵盤程式碼:

   mEditText.setFocusable(true);
   mEditText.setFocusableInTouchMode(true);
   mEditText.requestFocus();
   getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

關閉軟鍵盤程式碼:

  InputMethodManager imm = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);