android虚拟键盘弹出遮挡登陆按钮问题的解决方法
短信预约 -IT技能 免费直播动态提醒
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);
}
以上做了仔细说明了,运行效果如下:
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341