Android虚拟键盘的弹起会遮挡住部分ui,虽然通过在清单文件中设置,可以随着虚拟键盘的弹出,布局往上推,但是面对登陆界面时,并没有太大的作用,这样就会导致用户体验不好;开发中既然出现了就的解决;先说先解决的思路:获取到屏幕的高度、虚拟键盘的高度,布局的高度,用屏幕的高度减去布局的高度,用高度差和虚拟键盘的高度进行对比;代码实现如下;
private LinearLayout logo_layout;
private ImageView iv_logo;
private int sh;
private int layoutH;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logo_layout=(LinearLayout) findViewById(R.id.logo_layout);
iv_logo=(ImageView) findViewById(R.id.iv_logo);
//获取屏幕的高度
DisplayMetrics dm = new DisplayMetrics();
WindowManager windowMgr = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
windowMgr.getDefaultDisplay().getMetrics(dm);
sh = dm.heightPixels;
logo_layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
// 当layout执行结束后回调此方法
@Override
public void onGlobalLayout() {
logo_layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
layoutH = logo_layout.getHeight();
}
});
//当键盘弹起的时候用屏幕的高度减去布局的高度,同时获取到键盘的高度,用键盘的高度和剩余的高度做对比
SoftKeyBoardListener.setListener(MainActivity.this, new OnSoftKeyBoardChangeListener() {
@Override
public void keyBoardShow(int height) {
//键盘弹起回调
int h=sh-layoutH;
if(h>height){//高度大于键盘的高度
setLayoutH(80);
}else{
//高度小于键盘的高度
int resetH=Math.abs(height+layoutH-sh);
setLayoutH(resetH);
}
}
@Override
public void keyBoardHide(int height) {
//键盘隐藏回调
setLayoutH(80);
}
});
}
private void setLayoutH(int h){
LinearLayout.LayoutParams layoutParams = (android.widget.LinearLayout.LayoutParams) iv_logo.getLayoutParams();
layoutParams.topMargin=dip2px(MainActivity.this, h);
iv_logo.setLayoutParams(layoutParams);
}
public static int dip2px(Context context,float dpValue) {
final float scale =context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
private View rootView;//activity的根视图
int rootViewVisibleHeight;//纪录根视图的显示高度
private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
public SoftKeyBoardListener(Activity activity) {
//获取activity的根视图
rootView = activity.getWindow().getDecorView();
//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//获取当前根视图在屏幕上显示的大小
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int visibleHeight = r.height();
if (rootViewVisibleHeight == 0) {
rootViewVisibleHeight = visibleHeight;
return;
}
//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
if (rootViewVisibleHeight == visibleHeight) {
return;
}
//根视图显示高度变小超过200,可以看作软键盘显示了
if (rootViewVisibleHeight - visibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
//根视图显示高度变大超过200,可以看作软键盘隐藏了
if (visibleHeight - rootViewVisibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
}
});
}
private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
}
public interface OnSoftKeyBoardChangeListener {
void keyBoardShow(int height);
void keyBoardHide(int height);
}
public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
}
以上做了仔细说明了,运行效果如下: