android 如何隐藏软键盘?

jecbmhm3  于 2023-01-11  发布在  Android
关注(0)|答案(2)|浏览(193)

当focusing edittext时,我想在范围外敲击时隐藏软键盘。
MainActivity.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,它可以工作,但不能滚动
xxFragment.kt

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

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

tkclm6bt

tkclm6bt1#

您需要覆盖“活动”的dispatchTouchEvent()方法。
只要在你的活动中写这段代码,它就会在你触摸EditText之外的地方隐藏软键盘。不需要在任何地方调用任何函数。

/**
 * 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
}
n3schb8v

n3schb8v2#

id分配给ConstraintLayout

<ConstraintLayout
    android="@+id/cl_container" >
    <ScrollView>
        <LinearLayout>

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

对于你的点击监听器,

binding.clContainer.setOnClickListener{
     val inputMethodManager: InputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
     inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}

相关问题