英文:
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
<ConstraintLayout>
<ScrollView>
<LinearLayout>
</LinearLayout>
</ScrollView>
</ConstraintLayout>
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 ->
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
}
答案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
,
<ConstraintLayout
android="@+id/cl_container" >
<ScrollView>
<LinearLayout>
</LinearLayout>
</ScrollView>
</ConstraintLayout>
For your click listener,
binding.clContainer.setOnClickListener{
val inputMethodManager: InputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论