EditText.setFocusable(true); EditText.setFocusableInTouchMode(true); EditText.requestFocus(); InputMethodManager inputManager = (InputMethodManager) viewHolder.commentEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(EditText, 0);
android:windowSoftInputMode的值adjustPan或者adjustResize便可,像这样:android
<activity android:name=".MainActivity" android:windowSoftInputMode="adjustPan" > ... </activity>
这个方法在一些ListView里使用的时候,输入框依然会被遮盖.app
<activity android:name=".work.HomeActivity"
android:windowSoftInputMode="adjustPan"/>
而后是在布局文件里的输入框,由于是点击按钮后显示输入框,因此下面的输入框在布局最下面为隐藏状态,ide
<EditText android:id="@+id/comment_edittext" android:layout_width="match_parent" android:layout_height="52dp" android:textSize="@dimen/font_size_14" android:hint="评论" android:textColorHint="@color/fontColorAuxiliaryLightGray" android:singleLine="true" android:visibility="gone" android:focusable="true" android:focusableInTouchMode="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"/>
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; if (mEdittext.getVisibility() == View.GONE){//此处多添加一次显示,由于OnGlobalLayoutListener的连续性会致使以前未触发键盘的判断还执行,而后又隐藏了输入框 mEdittext.setVisibility(View.VISIBLE); mEdittext.setFocusable(true); mEdittext.setFocusableInTouchMode(true); mEdittext.requestFocus(); mEdittext.setTag(true); } L.e("已经触发键盘"); }else { //获取输入法是否要显示的状态,注意别觉得能够使用mEdittext.getVisibility()来替代,其实是不行的, //由于OnGlobalLayoutListener的监听是连续触发而且有延迟,很容易在mEdittext显示的一瞬间就隐藏了 if ((boolean)mEdittext.getTag()){ mEdittext.setVisibility(View.GONE); mEdittext.setTag(false); } L.e("没有触发键盘"); } } }; mRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);//给xml里的根布局设置监听 }
mBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mEdittext.setFocusable(true); mEdittext.setFocusableInTouchMode(true); mEdittext.requestFocus(); InputMethodManager inputManager = (InputMethodManager) mCommentEdittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);//获取输入法管理 inputManager.showSoftInput(mEdittext, 0);//要显示输入法的view mEdittext.setTag(true);//给输入框设一个标记,标示输入框已经显示 } });
最后注意移除监听布局
@Override public void onPause() { super.onPause(); mRootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(mGlobalLayoutListener); }
endspa