LazyKeyboard icon indicating copy to clipboard operation
LazyKeyboard copied to clipboard

自定义的软键盘如果遮盖住了输入框,怎么把布局顶起来?

Open Karthine opened this issue 6 years ago • 3 comments

Karthine avatar Apr 03 '19 10:04 Karthine

这个安全键盘的实现是基于PopupWindow,所以布局遮挡的问题需要个人处理下。或者你把场景发出来我帮你看下

onlyloveyd avatar Jun 02 '19 06:06 onlyloveyd

我也遇到并解决了这个问题,理论上说可以放到键盘实现类里面去做,让调用者不用关心内部细节。 思路就是:计算触发该键盘的editTExt距离屏幕底部距离,由于我们的键盘是在屏幕底部(至少在我的应用场景中是)弹出的,因此,只要这个距离只要小于键盘高度,即被遮挡,那么只需要将父布局向上滚动被遮挡的高度即可。 `private void adjustPan(final View root,final View trigger){ scrollToPosition = 0; root.postDelayed(new Runnable() { @Override public void run() { int[] loc = new int[2]; trigger.getLocationOnScreen(loc); int triggerToBtm = DisplayUtils.getScreenHeight(m_cxtParent) - loc[1] - trigger.getHeight(); int keyboardHeight = vContainer.getHeight(); //LogUtil.e(TAG," m_vKeyboard:" + keyboardHeight + " triggerToBtm:"+triggerToBtm);

            if (triggerToBtm < keyboardHeight){
                scrollToPosition = vContainer.getHeight() - triggerToBtm;
                root.scrollTo(0,scrollToPosition);
                isScrolled = true;
            }else {
                scrollToPosition = 0;
            }
        }
    },300);

}`

另外,需要监听dismiss,并在dimiss回调中处理父布局回滚到正常位置: setOnDismissListener(new OnDismissListener() { @Override public void onDismiss() { if (isScrolled){ m_vgParentLayout.postDelayed(new Runnable() { @Override public void run() { m_vgParentLayout.scrollTo(0,0); isScrolled = false; } },10); } } });

androidneko avatar Aug 06 '19 07:08 androidneko

我的edittext是放在activity中的,可以这样处理。(其他情况,比如scrollview或者可滑动列表没有试,但是也可以用类似的方法处理)
在KeyboardDialog.java的show()方法中,监听dialog的弹出与隐藏,弹出后,获取dialog和edittext的坐标,如果edittext的坐标y+edittext高度 > dialog的坐标,就相当于edittext被挡住了,此时将页面根布局整体上移就可以了。

我用的kotlin,就没转java了,你应该能看懂。

    var scrolled = false
    dialog.setOnDismissListener {
        if (scrolled) {
            val activity = context as Activity
            val root = activity.window.decorView.findViewById<View>(android.R.id.content)
            root.postDelayed({
                root.scrollTo(0, 0)
            }, 50)
        }
        scrolled = false
    }
    dialog.setOnShowListener {
        val view = dialog.window!!.decorView
        val location = IntArray(2)
        editText.getLocationOnScreen(location)
        val editTextHeight = location[1]
        view.getLocationOnScreen(location)
        val dialogHeight = location[1]
        if (editTextHeight + editText.height > dialogHeight) {
            scrolled = true
            val activity = context as Activity
            val root = activity.window.decorView.findViewById<View>(android.R.id.content)
            root.postDelayed({
                root.scrollTo(0, editTextHeight - dialogHeight + editText.height)
            }, 50)
        }
    }
    dialog.show()

YistXin avatar Feb 11 '22 01:02 YistXin