安卓:检测键盘是否打开,onApplyWindowListener不起作用

8
我试图在键盘打开时隐藏布局底部的一个特定按钮,以便让用户获得更多的视图可用性。
随着 androidx.core:core-ktx:1.5.0-alpha02 的发布,Google(终于)添加了一个名为 insets.isVisible(WindowInsetsCompat.Type.ime()) 的方法,该方法返回一个布尔值,指示键盘是否已打开或单击。
我使用一个基类 EmailFragment,在其中设置函数以实现上述功能。我的问题是我的 ViewCompat.setOnApplyWindowInsetsListener(view) 从未被调用(没有toast等)。
我还尝试直接在使用的片段中设置 ViewCompat.setOnApplyWindowInsetsListener(view),但这没有改变任何东西。
我的最小API为21,在我的AndroidManifest.XML中,我有 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(已打码)

在此输入图片描述

屏幕截图2,无法工作(已打码)

在此输入图片描述

我知道使用 android:windowSoftInputMode = adjustPen 会使我的按钮“不可见”,但这样我就不能再滚动了,很遗憾..

另一个解决方案可能是让键盘覆盖在按钮上,但我不知道如何做到...

非常感谢您的帮助,谢谢。


你正在使用哪个版本的Android进行测试? - Pankaj Kumar
@PankajKumar 我正在使用在Android 10上运行的模拟器进行测试。 - Andrew
1
@Andrew,你找到解决方法了吗?我也遇到了同样的问题。 - dakshbhatt21
1
很遗憾,正如上面所说,这个函数要么根本不起作用(因为是alpha版),要么只适用于安卓11的设备。我会尝试后续调查,但我没有希望它能够正常工作。 - Andrew
@Andrew 好的,谢谢! - dakshbhatt21
显示剩余5条评论
1个回答

3
我能够使这个工作正常是通过以下步骤实现的:
  1. 在清单文件中的Activity标签中添加android:windowSoftInputMode="adjustResize"
  2. window.decorView上设置OnApplyWindowInsetsListener
然而,这对状态栏产生了不良影响,这意味着OP的评论(“我不抱希望这会起作用”)可能是准确的。希望当它从alpha和beta版本升级时,可以解决这个问题。

我按照您的指南使其工作了,但是我不知道如何在Kotlin中将监听器设置为空。我需要在顶部声明变量为“private var decore: ??”,这样我就可以在onDestroyView中访问并将其设置为空,否则当我移动到另一个片段时会导致空值。 - AndroidDev123

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接