方法一:在你的activity中的oncreate中setContentView以前寫上這個代碼html
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
方法二:在項目的AndroidManifest.xml文件中界面對應的<activity>里加入,這樣會讓屏幕總體上移。android
android:windowSoftInputMode="stateVisible|adjustResize"
若是加上的是這樣鍵盤就會覆蓋屏幕。ide
android:windowSoftInputMode="adjustPan"
方法三:把頂級的layout替換成ScrollView,或者說在頂級的Layout上面再加一層ScrollView的封裝。這樣就會把軟鍵盤和輸入框一塊兒滾動了,軟鍵盤會一直處於底部。this
// 點擊空白區域 自動隱藏軟鍵盤 public boolean onTouchEvent(MotionEvent event) { if(null != this.getCurrentFocus()){ /** * 點擊空白位置 隱藏軟鍵盤 */ InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0); } return super .onTouchEvent(event); }
//此方法只是關閉軟鍵盤 能夠在finish以前調用一下 xml
//此方法只是關閉軟鍵盤 能夠在finish以前調用一下 private void hintKbTwo() { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); if(imm.isActive()&&getCurrentFocus()!=null){ if (getCurrentFocus().getWindowToken()!=null) { imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } }
//此方法,若是顯示則隱藏,若是隱藏則顯示 private void hintKbOne() { InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); // 獲得InputMethodManager的實例 if (imm.isActive()) { // 若是開啓 imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); } }