对于全屏模式下, 默认edittext是不会随着软键盘的弹出而向上移动的, 即使在xml里设置属性
android:windowSoftInputMode="stateVisible|adjustResize" 这个即可, 例如:
但是在设置了全屏模式的情况下, 即使设置如上属性, 也不起作用, 运行的结果同样如下:
怎么解决这个问题呢,
我的方法是通过属性动画, 让edittext移动到软键盘的上方, 因为属性动画是真正的移动控件, 这样再使用键盘输入, 输入结果会显示在editext上, 共有一下几步:
1.计算出软键盘的高度
2.判断软键盘是否弹出
3.计算出edittext需要移动的距离
4.如果弹出, 给edittext设置动画移动到软键盘的上方
5.如果软键盘收回, 则给edittext设置动画移动回原来的位置:
计算的方式如下:
edit_main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
private int statusBarHeight;
@Override
public void onGlobalLayout() {
Log.d(TAG, "onGlobalLayout: ");
Rect r = new Rect();
// 使用最外层布局填充,进行测算计算
rl_main.getWindowVisibleDisplayFrame(r);
int screenHeight = myLayout.getRootView().getHeight();
int heightDiff = screenHeight - (r.bottom - r.top);
if (heightDiff > 100) {
// 如果超过100个像素,它可能是一个键盘。获取状态栏的高度
statusBarHeight = 0;
}
try {
Class> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = MainActivity.this.getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
// 软键盘的高度
int realKeyboardHeight = heightDiff - statusBarHeight;
if (realKeyboardHeight > 400 && !isEditUp) {
isEditUp = true;
putEditTextUp(realKeyboardHeight);
distance_to_move = realKeyboardHeight - getVirtualBarHeigh();
} else if(realKeyboardHeight < 400 && !isEditDown) {
isEditDown = true;
putEditDown(distance_to_move);
}
}
});
判断其实很简单, 就是根据第一步拿到的高度, 我是如果大于400就认为弹出了软键盘,
就是软键盘的高度 - 虚拟按键的高度(如果有的话)
distance_to_move = realKeyboardHeight - getVirtualBarHeigh();
/**
* 获取虚拟按键的高度
* @return
*/
public int getVirtualBarHeigh() {
int vh = 0;
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
try {
@SuppressWarnings("rawtypes")
Class c = Class.forName("android.view.Display");
@SuppressWarnings("unchecked")
Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
method.invoke(display, dm);
vh = dm.heightPixels - windowManager.getDefaultDisplay().getHeight();
} catch (Exception e) {
e.printStackTrace();
}
return vh;
}
给edittext设置一个属性动画, 让其按指定的距离移动
ValueAnimator anim = ValueAnimator.ofInt(0, realKeyboardHeight - getVirtualBarHeigh());
anim.setDuration(10);
anim.setRepeatCount(0);
anim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
isEditDown = false;
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int currentValue = (Integer) animation.getAnimatedValue();
// 获得改变后的值
System.out.println(currentValue);
// 输出改变后的值
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) edit_main.getLayoutParams();
layoutParams.setMargins(0, 0, 0, currentValue);
edit_main.setLayoutParams(layoutParams);
edit_main.requestLayout();
}
});
anim.start();
ValueAnimator anim = ValueAnimator.ofInt(distance_to_move, 0);
anim.setDuration(10);
anim.setRepeatCount(0);
anim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
isEditUp = false;
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int currentValue = (Integer) animation.getAnimatedValue();
// 获得改变后的值
System.out.println(currentValue);
// 输出改变后的值
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) edit_main.getLayoutParams();
layoutParams.setMargins(0, 0, 0, currentValue);
edit_main.setLayoutParams(layoutParams);
edit_main.requestLayout();
}
});
anim.start();
我的布局文件如下:
运行结果如下:
Demo下载地址: https://download.csdn.net/download/c1392851600/10969281