隐藏软键盘的方法是什么?

huangapple go评论63阅读模式
英文:

How do I hide the soft keyboard?

问题

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

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
    }

输入法管理器(Input Method Manager)位于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)
}

我尝试了上述代码但失败了。

英文:

While foucusing edittext, I would like to hide the soft keyboard when tapping outside the range.

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
    }

Input Method Manager is written in MainActivity

in a xml

&lt;ConstraintLayout&gt;
    &lt;ScrollView&gt;
        &lt;LinearLayout&gt;

        &lt;/LinearLayout&gt;
    &lt;/ScrollView&gt;
&lt;/ConstraintLayout&gt;

When I create this layout, the keyboard does not hide when I tap outside the range
How can I hide the keyboard?

If I write only ConstraintLayout it works but no scrolling

xxFragment.kt

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

I tried the above code and it failed

答案1

得分: 0

您需要重写Activity的dispatchTouchEvent()方法。

只需在您的Activity中编写以下代码,它将在触摸EditText之外的区域时立即隐藏软键盘。无需在任何地方调用任何函数。它将在Activity的所有片段上工作。

/**
 * 点击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.left - touchCoordinates[0]
                        val y: Float = event.rawY + view.top - touchCoordinates[1]
                        // 如果触摸位置在EditText之外,则隐藏键盘
                        if (x < view.left || x >= view.right || y < view.top || y >= view.bottom) {
                            val imm =
                                getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                            imm.hideSoftInputFromWindow(view.windowToken, 0)
                            view.clearFocus()
                        }
                    }
                }
            }
        }
    }
    return ret
}
英文:

You need to override dispatchTouchEvent() method of Activity.

Just write this code in your activity and it will hide soft keyboard as soon as you touch outside EditText. No need to call any function anywhere. 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 -&gt;
        if (ev.action == MotionEvent.ACTION_DOWN) {
            pressTime = System.currentTimeMillis()
        } else if (event.action == MotionEvent.ACTION_UP) {
            val releaseTime = System.currentTimeMillis()
            if (releaseTime - pressTime &lt; 100) {
                currentFocus?.let { view -&gt;
                    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 &lt; view.getLeft() || x &gt;= view.getRight() || y &lt; view.getTop() || y &gt; view.getBottom()) {
                            val imm =
                                getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                            imm.hideSoftInputFromWindow(view.windowToken, 0)
                            view.clearFocus()
                        }
                    }
                }
            }
        }
    }
    return ret
}

答案2

得分: 0

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)
}
英文:

Assign id to ConstraintLayout,

&lt;ConstraintLayout
    android=&quot;@+id/cl_container&quot; &gt;
    &lt;ScrollView&gt;
        &lt;LinearLayout&gt;

        &lt;/LinearLayout&gt;
    &lt;/ScrollView&gt;
&lt;/ConstraintLayout&gt;

For your click listener,

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

huangapple
  • 本文由 发表于 2023年1月9日 17:35:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定