您当前所在位置:首页软件下载yetu:android全屏下,如何让软键盘只顶起输入框

yetu:android全屏下,如何让软键盘只顶起输入框

更新:2024-08-09 21:07:02编辑:BOSS软件库归类:软件下载人气:858
欧意国内注册

android全屏下,如何让软键盘只顶起输入框

android软键盘状态监听最稳的方法,属性动画手动调整布局

最近忙于自媒体事业不能自拔,已经很久没有写技术博客了。之所以写是因为这个非常的重要,也非常的好用(软键盘冲突的问题总是非常的让安卓程序员头疼)。先上一张完美的效果图。

yetu:android全屏下,如何让软键盘只顶起输入框

//监听键盘展开 、收起 private void listenerKeyBoard() { getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { //当页面布局发生变化的时候调用 YetuLog.e("onLayoutChange", "onLayoutChange: " + "调用"); //因为addOnGlobalLayoutListener是在页面加载完成后才调用,所以当前获取的View参数都是真实有效 int[] location = new int[2]; rlMiddle.getLocationOnScreen(location); rl_height = location[1]; setViewHeight(); } }); } private boolean isSoftShowing() { //获取当前屏幕内容的高度 int screenHeight = getWindow().getDecorView().getHeight(); //获取View可见区域的bottom Rect rect = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); return screenHeight - rect.bottom != 0; } private void setViewHeight() {// YetuLog.e("onLayoutChange", "onLayoutChange: " + rl_height); if (isSoftShowing()) { //软键盘弹出// YetuLog.e("onLayoutChange", "onLayoutChange: " + "弹出"); //rl_height==0 页面已经上移,不再向上移动(这里是对非软键盘导致的布局切换变化做一个判断) if (rl_height != 0) { ObjectAnimator translationY = new ObjectAnimator().ofFloat(rlMiddle, "translationY", 0, -layTop.getBottom()); //在这里0表示当前控件的位置 translationY.setDuration(200); translationY.start(); } } else { //软键盘收起// YetuLog.e("onLayoutChange", "onLayoutChange: " + "收起"); if (rl_height == 0) { ObjectAnimator translationY = new ObjectAnimator().ofFloat(rlMiddle, "translationY", -layTop.getBottom(), 0); translationY.setDuration(200); translationY.start(); } } } @Override protected void onResume() { super.onResume(); //addOnGlobalLayoutListener 判断页面是否已经加载完毕 getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { /** * Callback method to be invoked when the global layout state or the visibility of views * within the view tree changes */ @Override public void onGlobalLayout() { //判断是否第一次进入页面 if (!isLoaded) { isLoaded = true; listenerKeyBoard(); }else{ int[] location = new int[2]; rlMiddle.getLocationOnScreen(location); rl_height = location[1];// YetuLog.e("onLayoutChange", "onLayoutChange: " + "重新初始化" + rl_height); setViewHeight(); } getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this); } }); }}

大概讲一下逻辑吧。首先是执行onResume方法里的代码,这里面isLoaded是一个标记,只有在第一次进入该页面的时候,才会调用listenerKeyBoard方法,启动onLayoutChange监听。else的情况是在进入下一个页面,然后再返回有编辑框的时候,直接调用判断软键盘状态的方法setViewHeight()同时设置页面动画。这其中,lay_top是那个带有返回键的控件,rlMiddle这是那整块在移动的控件。调用addOnGlobalLayoutListener是为了确认布局已经加载完成,这样才能准确的获取控件宽高之类的参数。其中,判断软键盘状态的核心方法是isSoftShowing()。剩下的代码注释都有,布局文件就不贴出来了。

yetu:android全屏下,如何让软键盘只顶起输入框

上图是附录测试log。该套方法主要用于,在页面没有ScrollView或者RecyclerView之类的列表或者EditView太高,而其他的控件又太低,没办法把底下的这些控件顶起来,只能手动移动控件的时候。之所以使用属性动画,没有直接onlayout(),是考虑到整个布局的平滑过渡,使用属性动画的体验会更好些。

yetu:android全屏下,如何让软键盘只顶起输入框

到此,以上就是小编对于yetu的问题就介绍到这了,希望介绍关于yetu的1点解答对大家有用。

Amysql_youhua_articlehuaunyuan($article);
yetu
陈梦早期的短发照片:综合分析:你觉得陈梦问鼎乒乓球亚洲杯女单冠军的几率大吗?为什么 日本发巨大地震预警:日本真的地震了吗
欧意国内注册 欧易国际注册 欧意交易所app官方下载

游客 回复需填写必要信息