【问题标题】:How do I hide the soft keyboard?如何隐藏软键盘?
【发布时间】:2023-01-09 18:52:10
【问题描述】:

在关注编辑文本时,我想在范围外点击时隐藏软键盘。

主活动.kt

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (currentFocus != null) {
            val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

            inputMethodManager.hideSoftInputFromWindow(
                // container is ConstraintLayout
                binding.container.windowToken,
                InputMethodManager.HIDE_NOT_ALWAYS
            )
        }
        return false
    }

输入法管理器写在MainActivity中

在一个 xml 中

<ConstraintLayout>
    <ScrollView>
        <LinearLayout>

        </LinearLayout>
    </ScrollView>
</ConstraintLayout>

创建此布局时,当我在范围外点击时键盘不会隐藏 如何隐藏键盘?

如果我只写 ConstraintLayout 它可以工作但没有滚动

xx片段.kt

scrollView.setOnClickListener {
    val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(binding.scrollView.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}

我试过上面的代码,但失败了

【问题讨论】:

    标签: android kotlin android-layout scrollview


    【解决方案1】:

    您需要覆盖 Activity 的 dispatchTouchEvent() 方法。

    只需在您的 Activity 中编写此代码,只要您触摸 EditText 之外,它就会隐藏软键盘。无需在任何地方调用任何函数。 It will work on all the fragments of the activity

    /**
     * Hide Keyboard when tapped outside editText
     */
    private var pressTime: Long = 0L
    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        val ret = super.dispatchTouchEvent(ev)
        ev?.let { event ->
            if (ev.action == MotionEvent.ACTION_DOWN) {
                pressTime = System.currentTimeMillis()
            } else if (event.action == MotionEvent.ACTION_UP) {
                val releaseTime = System.currentTimeMillis()
                if (releaseTime - pressTime < 100) {
                    currentFocus?.let { view ->
                        if (view is EditText) {
                            val touchCoordinates = IntArray(2)
                            view.getLocationOnScreen(touchCoordinates)
                            val x: Float = event.rawX + view.getLeft() - touchCoordinates[0]
                            val y: Float = event.rawY + view.getTop() - touchCoordinates[1]
                            //If the touch position is outside the EditText then we hide the keyboard
                            if (x < view.getLeft() || x >= view.getRight() || y < view.getTop() || y > view.getBottom()) {
                                val imm =
                                    getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                                imm.hideSoftInputFromWindow(view.windowToken, 0)
                                view.clearFocus()
                            }
                        }
                    }
                }
            }
        }
        return ret
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      相关资源
      最近更新 更多