需求:
现在有一个需求是点击一行文本框,弹出一个之前隐藏的输入框,输入完成后按返回键或者其他的东西隐藏键盘和输入框,将输入框的内容填充到文本框中。
实现:
拿到这个需求的第一反应就是写一个监听来监听键盘的显示和隐藏来控制输入框的显示和隐藏,控制文本框中的内容。
所以我做了如下操作:
@override public void onlayoutchange(view v, int left, int top, int right, int bottom, int oldleft, int oldtop, int oldright, int oldbottom) { //old是改变前的左上右下坐标点值,没有old的是改变后的左上右下坐标点值 //现在认为只要控件将activity向上推的高度超过了1/3屏幕高,就认为软键盘弹起 if(oldbottom != 0 && bottom != 0 &&(oldbottom - bottom > keyheight)){ toast.maketext(mainactivity.this, "监听到软键盘弹起...", toast.length_short).show(); }else if(oldbottom != 0 && bottom != 0 &&(bottom - oldbottom > keyheight)){ toast.maketext(mainactivity.this, "监听到软件盘关闭...", toast.length_short).show(); } }
问题:
没错,这样确实是能够做到监听软键盘的弹出和隐藏,这一切都是因为之前设置了indowsoftinputmode=adjustresize,但是当全屏模式下是这个属性是无效的,键盘弹出和隐藏并不会触发onlayouchangelistener。
而项目中使用了systembartintmanager之后,activity就变成了全屏模式所以我做了如下操作
//contentlayout是最外层布局 mchildofcontent = contentlayout.getchildat(0); mchildofcontent.getviewtreeobserver() .addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { public void ongloballayout() { possiblyresizechildofcontent(); }});
private void possiblyresizechildofcontent() { int usableheightnow = computeusableheight(); if (usableheightnow != usableheightprevious) { int usableheightsanskeyboard = mchildofcontent.getrootview().getheight(); int heightdifference = usableheightsanskeyboard - usableheightnow; if (heightdifference > (usableheightsanskeyboard / 4)) { // 键盘弹出 } else { // 键盘收起 productinfo.setvisibility(view.gone); productinfoend.settext(productinfo.gettext().tostring()); } mchildofcontent.requestlayout(); usableheightprevious = usableheightnow; } }
private int computeusableheight() { rect r = new rect(); mchildofcontent.getwindowvisibledisplayframe(r); return (r.bottom - r.top);}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
Xamarin 安卓 相机 各种坑 目标API Android 6以后,我选的目标API版本是 Android 10
Android 9.0+软件某些功能突然暴毙:cleartext not permitted
Android : Resource is not a Drawable (color or path)
Flutter问题集锦——Android Studio 4.0.1 在AVD Manger中启动模拟器报“unable to locate abd”
网友评论