场景底部edittext点击之后会上推整个布局
解决方案
/**
* 打开软键盘的
* @param editText
*/
private void showInputManager(EditText editText) {
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
/** 目前测试来看,还是挺准的
* 原理:OnGlobalLayoutListener 每次布局变化时都会调用
* 界面view 显示消失都会调用,软键盘显示与消失时都调用
* */
mRootView.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutChangeListener);
InputMethodManager inputManager =
(InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
mRootView指整个布局,
ViewTreeObserver.OnGlobalLayoutListener mLayoutChangeListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
int height = wm.getDefaultDisplay().getHeight();
//判断窗口可见区域大小
Rect r = new Rect();
// getWindowVisibleDisplayFrame()会返回窗口的可见区域高度
getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
//如果屏幕高度和Window可见区域高度差值大于整个屏幕高度的1/3,则表示软键盘显示中,否则软键盘为隐藏状态。
int heightDifference = height - (r.bottom - r.top);
boolean isKeyboardShowing = heightDifference > height / 3;
if (isKeyboardShowing) {
// D.i("slack","show..."+ r.bottom + " - " + r.top + " = " + (r.bottom - r.top) +","+ heightDifference);
// bottomView 需要跟随软键盘移动的布局
// setDuration(0) 默认300, 设置 0 ,表示动画执行时间为0,没有过程,只有动画结果了
buttomLlDetails.setVisibility(View.GONE);
etEnter.setVisibility(View.VISIBLE);
etEnter.setFocusable(true);
etEnter.setFocusableInTouchMode(true);
etEnter.requestFocus();
//上边这些不重要 主要是下边的 buttomSvDetails是包裹我的edittext的父控件一个scrollview让edittext移动软键盘的距离,是这个控件可以一直保持在软键盘的上边
buttomSvDetails.animate().translationY(-heightDifference).setDuration(0).start();
} else {
// D.i("slack","hide...");
buttomSvDetails.animate().translationY(0).setDuration(0).start();
if(mToolbar.getVisibility()==View.VISIBLE){
buttomLlDetails.setVisibility(View.VISIBLE);
}
etEnter.setVisibility(View.GONE);
}
}
};
<ScrollView
android:id="@+id/sv_comment"
android:padding="@dimen/y9"
android:background="#348EB4D9"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:padding="@dimen/y5"
android:background="@drawable/shape_bg_et_details"
android:id="@+id/et_comment"
android:imeOptions="actionSend"
android:singleLine="true"
android:inputType="text"
android:focusableInTouchMode="false"
android:hint="写评论"
android:editable="false"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
布局文件 要跟随软键盘移动的控件放到scrollview的里边