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