您的位置:首页 > 移动开发 > Android开发

键盘弹起布局上移的问题

2016-12-26 11:58 1201 查看

简述

adjustResize关于这个属性其实大家用的很多,就是当我们的键盘弹出时,我们的layout会重新布局。

但是这个属性不是所有情况都适用的。

下面讲讲我遇到的问题 以及解决办法。

我们以常见的评论框为例子。

1、我们先说正常的情况,很简单的布局。点击一个button然后弹出输出框。

Activity的属性

<activity
android:name=".acitivity.KeyboardActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateHidden"
>
</activity>


Activity_layout布局,弹出键盘后如下图,这个是正常的



但是有一种情况,如果我的布局设置足够大的高度时,比如 根布局的高度被定死这个高度

android:layout_height="500dp"


(这里说明一个问题,因为要做一个小红书APP的图文详情的页面。我是用了CoordinatorLayout+viewpager+recyclerView,因为图片最高的大概要有8成屏的高度,所以遇到了adjustresize属性设置后,底部的输入框依然被键盘遮挡住了)

这个时候显示就变成就不正常了,看不到上面输入框了。如下图所示:



那是因为布局定死了高度,即使我们设置了adjustResize属性,依然无法达到预期的效果。

解决办法:采用PopupWindow的方式来弹出评论输入框

下面给出代码:

Activity的属性 使用adjustNothing即可

<activity
android:name=".acitivity.KeyboardActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="使用adjustNothing即可|stateHidden"
>
</activity>


弹出PopupWindow:

PopupWindow windowComment;
View viewComment;
EditText winEditText;
public void showWindowComment(View view) {
if (windowComment == null) {
viewComment = View.inflate(this, R.layout.window_comment_test, null);
windowComment = new PopupWindow(viewComment, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, true);
//设置动画效果
//windowComment.setAnimationStyle(R.style.popwindow_anim);
//点击其他地方消失
windowComment.setOutsideTouchable(true);
windowComment.setFocusable(true);
windowComment.setBackgroundDrawable(new ColorDrawable(0));
//这里让设置下PopupWindow的SoftInputMode
windowComment.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
windowComment.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
winEditText = (EditText) viewComment.findViewById(R.id.etWin);
ResizeLayout resizeLayout = (ResizeLayout) viewComment.findViewById(R.id.resizeLayout);
resizeLayout.setOnKeyBoardListener(new ResizeLayout.onKeyBoardListener() {
@Override
public void onKeyBoardShow(int nowHeight) {

}

@Override
public void onKeyBoardHide(int nowHeight) {
windowComment.dismiss();

}
});
resizeLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return false;
}
});
}
//自动弹起键盘
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
winEditText.setFocusable(true);
winEditText.setFocusableInTouchMode(true);
winEditText.requestFocus();
InputMethodManager inputManager =
(InputMethodManager)winEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(winEditText, 0);
}

}, 100);

windowComment.showAtLocation(view, Gravity.BOTTOM, 0, 0);
}


ResizeLayout,网上写的监听键盘弹出的布局。

用于监听软键盘的弹起和隐藏

public class ResizeLayout extends RelativeLayout{

private onKeyBoardListener listener;

public void setOnKeyBoardListener(onKeyBoardListener listener) {
this.listener = listener;
}

public ResizeLayout(Context context) {
super(context);
}

public ResizeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public ResizeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
//LogUtil.println("********" + h + "*********" + oldh);
if (oldh == 0 || h == 0) {
return;
}
if (oldh - h > BaseActivity.dip2px(getContext(), 100)) {
if (listener != null) {
listener.onKeyBoardShow(h);
return;
}
}
if (listener != null) {
listener.onKeyBoardHide(h);
}
}

public interface onKeyBoardListener{
void onKeyBoardShow(int nowHeight);
void onKeyBoardHide(int nowHeight);
}
}


PS:自动弹起键盘参考网上代码。

总结:其实这个问题不复杂,开始的时候没想到利用弹出窗来实现。在这里做个记录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android