InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(view,InputMethodManager.SHOW_FORCED); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //強制隱藏鍵盤
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
這個方法有點不太可靠android
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裏的根佈局設置監聽 }
記得移除監聽ide
@Override public void onPause() { super.onPause(); mRootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(mGlobalLayoutListener); }
在清單文件對應的activity配置中加入一句Android:windowSoftInputMode="stateVisible|adjustResize"佈局
android:windowSoftInputMode的值adjustPan或者adjustResize便可,像這樣:this
<activity
android:name=".MainActivity" android:windowSoftInputMode="adjustPan" > ... </activity>
endspa