Edittext点击外部区域隐藏软件盘

1
2
3
4
5
6
7
8
9
10
11
12
13
binding.root.setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
val focusedView: View? = activity?.currentFocus
if (focusedView is EditText) {
// 如果当前焦点在EditText上,并且点击位置不在EditText范围内,隐藏键盘
if (event.x < focusedView.getLeft() || event.x > focusedView.getRight() ||
event.y < focusedView.getTop() || event.y > focusedView.getBottom()) {
hideSoftInput()
}
}
}
false
}