方法一(如果輸入法在窗口上已經顯示,則隱藏,反之則顯示)
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
方法二(view為接受軟鍵盤輸入的視圖,SHOW_FORCED表示強制顯示)
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(view,InputMethodManager.SHOW_FORCED); //顯示輸入盤 imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN); //強制隱藏鍵盤
自動彈出輸入法
mCommentEdittext.setFocusable(true); mCommentEdittext.setFocusableInTouchMode(true); mCommentEdittext.requestFocus(); InputMethodManager inputManager = (InputMethodManager) mCommentEdittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(mCommentEdittext, 0);
調用隱藏系統默認的輸入法
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken()
, InputMethodManager.HIDE_NOT_ALWAYS);
WidgetSearchActivity是當前的Activity
注意!以上的隱藏是調用activity的getCurrentFocus().getWindowToken().但是,這個調用在華為品牌的手機上會出現空指針報錯.所以為了適配華為最安全的寫法還是添加一個view,如下:
mInputMethodManager.hideSoftInputFromWindow(mContentView.getWindowToken()
, InputMethodManager.HIDE_NOT_ALWAYS);//隱藏輸入法
獲取輸入法打開的狀態
這個方法有點不太可靠
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); boolean isOpen=imm.isActive();//isOpen若返回true,則表示輸入法打開
計算屏幕的方式監聽輸入法是否打開
碎片里調用
private void initGlobalLayoutListener(){ mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() { int mScreenHeight = 0; int mKeyboardHeight = 0; @Override public void onGlobalLayout() { Rect rect = new Rect(); // 測量當前窗口的顯示區域 ((Activity)getContext()).getWindow().getDecorView() .getWindowVisibleDisplayFrame(rect); if(mScreenHeight <= 0){ mScreenHeight = ((WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE)) .getDefaultDisplay().getHeight(); } //計算出軟鍵盤的高度 int keyboardHeight = mScreenHeight - rect.bottom; //如果keyboardHeight大於屏幕的五分之一, // 此時keyboardHeight有效,反之就是軟鍵盤已經關閉了。 if (Math.abs(keyboardHeight) > mScreenHeight / 5) { mKeyboardHeight = keyboardHeight; L.e("已經觸發鍵盤"); }else { L.e("沒有觸發鍵盤"); } } }; mRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);//給xml里的根布局設置監聽 }
正常activity調用
private void initGlobalLayoutListener(){ mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() { int mScreenHeight = 0; int mKeyboardHeight = 0; @Override public void onGlobalLayout() { Rect rect = new Rect(); // 測量當前窗口的顯示區域 DemoActivity.this.getWindow().getDecorView() .getWindowVisibleDisplayFrame(rect); if(mScreenHeight <= 0){ WindowManager windowManager = (WindowManager) DemoActivity.this.getSystemService(Context.WINDOW_SERVICE); mScreenHeight = windowManager.getDefaultDisplay().getHeight(); } //計算出軟鍵盤的高度 int keyboardHeight = mScreenHeight - rect.bottom; //如果keyboardHeight大於屏幕的五分之一, // 此時keyboardHeight有效,反之就是軟鍵盤已經關閉了。 if (Math.abs(keyboardHeight) > mScreenHeight / 5) { mKeyboardHeight = keyboardHeight; L.e("已經觸發鍵盤"); }else { L.e("沒有觸發鍵盤"); } } }; mRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);//給xml里的根布局設置監聽 }
記得移除監聽
@Override public void onPause() { super.onPause(); mRootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(mGlobalLayoutListener); }
進入activity就要顯示輸入法
在清單文件對應的activity配置中加入一句Android:windowSoftInputMode="stateVisible|adjustResize"
不顯示輸入法
android:windowSoftInputMode="stateHidden"
根據輸入法位置改變,改變輸入框位置
android:windowSoftInputMode的值adjustPan或者adjustResize即可,像這樣:
<activity
android:name=".MainActivity" android:windowSoftInputMode="adjustPan" > ... </activity>
- stateUnspecified:軟鍵盤的狀態並沒有指定,系統將選擇一個合適的狀態或依賴於主題的設置
- stateUnchanged:當這個activity出現時,軟鍵盤將一直保持在上一個activity里的狀態,無論是隱藏還是顯示
- stateHidden:用戶選擇activity時,軟鍵盤總是被隱藏
- stateAlwaysHidden:當該Activity主窗口獲取焦點時,軟鍵盤也總是被隱藏的
- stateVisible:軟鍵盤通常是可見的
- stateAlwaysVisible:用戶選擇activity時,軟鍵盤總是顯示的狀態
- adjustUnspecified:默認設置,通常由系統自行決定是隱藏還是顯示
- adjustResize:該Activity總是調整屏幕的大小以便留出軟鍵盤的空間
- adjustPan:當前窗口的內容將自動移動以便當前焦點從不被鍵盤覆蓋和用戶能總是看到輸入內容的部分
輸入法彈出后,不壓縮布局,直接覆蓋布局
<activity android:name=".MainActivity" android:windowSoftInputMode="adjustResize|adjustPan"/>
在Fragment里設置輸入法
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
end
