在android開發中,關於EditText自動獲取焦點彈出鍵盤,我們可能又是會有讓鍵盤自動彈出的需求,有時可能又會有不想讓鍵盤自動彈出的需求,下面是我所總結的兩種方法:
需求:EditText自動獲取焦點並彈出鍵盤,代碼:
EditText.setFocusable(true);
EditText.setFocusableInTouchMode(true);
EditText.requestFocus();
需求:EditText不會自動獲取焦點並且不會彈出鍵盤,代碼:
將其父控件設置:
Parent.setFocusable(true);
Parent.setFocusableInTouchMode(true);
//打開軟鍵盤InputMethodManager imm = (InputMethodManager)MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); //InputMethodManager.SHOW_FORCED
//關閉軟鍵盤imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);