Android:检测打开的键盘,onApplyWindowListener不起作用 [英] Android: Detect opened keyboard, onApplyWindowListener not working

查看:184
本文介绍了Android:检测打开的键盘,onApplyWindowListener不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在打开键盘时在布局底部隐藏一个特定的按钮,以便为用户提供更多视图.

I am trying to hide one specific button at the bottom of my layout when the keyboard is opened, in order to make more view available for the user.

随着androidx.core:core-ktx:1.5.0-alpha02的发布,谷歌(最终)添加了一个名为insets.isVisible(WindowInsetsCompat.Type.ime())的方法,该方法返回打开或单击键盘的布尔值.

With the release of androidx.core:core-ktx:1.5.0-alpha02 google (finally) added a method called insets.isVisible(WindowInsetsCompat.Type.ime()) which returns a boolean whether the keyboard is opened or clicked.

我正在使用基类EmailFragment,在其中设置函数以实现上述书面功能.我的问题是我的ViewCompat.setOnApplyWindowInsetsListener(view)从未被呼叫(没有吐司等).

I am using a base class EmailFragment where I set the function in order to achieve the above written functionality. My problem is that my ViewCompat.setOnApplyWindowInsetsListener(view) gets never called (no toast etc).

我也尝试过直接在使用的Fragments中设置ViewCompat.setOnApplyWindowInsetsListener(view),但这并没有改变.

I've also tried to set ViewCompat.setOnApplyWindowInsetsListener(view) directly in the used Fragments, but that changed nothing.

我的最小API是21,在我的AndroidManifest.XML中,我有android:windowSoftInputMode = adjustResize

My min API is 21, in my AndroidManifest.XML I have android:windowSoftInputMode = adjustResize

abstract class EmailFragment<out T: ViewDataBinding>(
    layout: Int,
    // ... some other stuff that is not necessary for the question
) : BaseFragment<T>(layout) {
    // ... some other stuff that is not necesarry for the question

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        hideButton(view)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    
    private fun hideButton(view: View) {
        ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
            val isKeyboardVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
            if (isKeyboardVisible) {
                btn.visibility = View.GONE
                Toast.makeText(requireContext(), "KEYBOARD OPEN", Toast.LENGTH_SHORT).show()
            } else {
                btn.visibility = View.VISIBLE
                Toast.makeText(requireContext(), "KEYBOARD CLOSED", Toast.LENGTH_SHORT).show()
            }

            // Return the insets to keep going down this event to the view hierarchy
            insets
        }
    }
}

从EmailFragment继承的片段(五分之一)

class CalibrateRepairMessageFragment(
    //... some other stuff that is not necessary for this question
) : EmailFragment<FragmentCalibrateRepairMessageBinding>(
    R.layout.fragment_calibrate_repair_message,
    //... some other stuff that is not necessary for this question
) {
    //... some other stuff that is not necessary for this question

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //... some other stuff that is not necessary for this question
    }

屏幕截图1(已审查)

我知道使用android:windowSoftInputMode = adjustPen会使我的按钮不可见",但后来我再也无法滚动了,很伤心.

I know that using android:windowSoftInputMode = adjustPen makes my button "invisible" but then I cannot scroll anymore, big sad..

另一种解决方案可能是键盘仅与按钮重叠,但是我不知道如何执行此操作...

Another solution could be that the keyboard just overlaps the button, but I have no clue how to do that...

感谢您的帮助.

推荐答案

我能够通过以下方式使它起作用:

I was able to get this to work by:

  1. 在清单的Activity标签中添加android:windowSoftInputMode="adjustResize"
  2. window.decorView上设置OnApplyWindowInsetsListener.
  1. Adding android:windowSoftInputMode="adjustResize" to Activity tag in manifest
  2. Setting OnApplyWindowInsetsListener on window.decorView.

但是,这对状态栏产生了不幸的副作用,这意味着OP的注释(我不希望这样做会起作用")可能是准确的.希望它从alpha和beta毕业后都可以使用.

However, this had unfortunate side effects to the statusbar which means that OP's comment ("I have no hope that this will ever work") is probably accurate. Hopefully it will work when it graduates from alpha and beta.

这篇关于Android:检测打开的键盘,onApplyWindowListener不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆