Android:检测打开的键盘,onApplyWindowListener 不起作用

标签 android android-layout kotlin

当键盘打开时,我试图在布局底部隐藏一个特定按钮,以便为用户提供更多 View 。
随着 androidx.core:core-ktx:1.5.0-alpha02 的发布google(终于)添加了一个名为 insets.isVisible(WindowInsetsCompat.Type.ime()) 的方法无论键盘是打开还是单击,它都会返回一个 bool 值。
我正在使用基类 EmailFragment这里我设置函数是为了实现上面写的功能。我的问题是我的 ViewCompat.setOnApplyWindowInsetsListener(view)永远不会被调用(没有 toast 等)。
我也试过设置ViewCompat.setOnApplyWindowInsetsListener(view)直接在使用的 fragment 中,但这并没有改变。
我的最小 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 继承的 fragment (五分之一)
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(审查)
enter image description here
屏幕截图 2,不工作(审查)
enter image description here
我知道使用 android:windowSoftInputMode = adjustPen使我的按钮“不可见”,但后来我不能再滚动了,很伤心..
另一种解决方案可能是键盘只是与按钮重叠,但我不知道该怎么做......
我感谢每一个帮助,谢谢。

最佳答案

我能够通过以下方式使其工作:

  • 添加 android:windowSoftInputMode="adjustResize"Activity list 中的标签
  • 设置OnApplyWindowInsetsListenerwindow.decorView .

  • 但是,这对状态栏产生了不幸的副作用,这意味着 OP 的评论(“我不希望这会奏效”)可能是准确的。希望当它从 alpha 和 beta 毕业时它会起作用。

    关于Android:检测打开的键盘,onApplyWindowListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63874010/

    相关文章:

    Android TranslateAnimation 按钮闪烁

    java - 使用 [java/lang/String] 进行混淆警告

    android - 以编程方式设置微调器选择

    java - Android 中的 Consumer-rules.pro 和 proguard-rules.pro 有什么区别?

    java - SimpleDateFormat 未在单元测试中返回预期值

    android - 如何使用带有自定义入口点的 FlutterFragment 在我的 Android 应用程序中嵌入 Flutter 布局?

    android - 在 android 中使用 CSS 定位事物

    java - 阻止用户多次单击 edittext 以打开多个对话框

    android - 为 android UI 试验 xmls。想知道是否有更好的方法来完成我的任务

    android - 为标记 fragment 找到意外的命名空间前缀 "xmlns"