android虚拟键盘弹出遮挡登陆按钮问题的解决方法
程序员文章站
2024-02-19 08:01:34
android虚拟键盘的弹起会遮挡住部分ui,虽然通过在清单文件中设置,可以随着虚拟键盘的弹出,布局往上推,但是面对登陆界面时,并没有太大的作用,这样就会导致用户体验不好;...
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); } /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ 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); }
以上做了仔细说明了,运行效果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。