獲取系統軟鍵盤manager:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
根據當前狀態來判斷是否隱藏
if (imm.isActive()&&imm.isAcceptingText()) {
//view.hideSoftInputFromWindow(mRecordBtn.getWindowToken(),0);
imm.hideSoftInputFromWindow(mRecordBtn.getWindowToken(),0);//隱藏軟鍵盤(強制)
}
狀態切換:若顯示則隱藏,若隱藏則顯示
imm.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
手動顯示
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
如何讓軟鍵盤在點擊除EditText以外隱藏
public void setupUI(View view) {//根view if (!(view instanceof EditText)) { view.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } return false; } }); } if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { View innerView = ((ViewGroup) view).getChildAt(i); setupUI(innerView); } } }