如图所示,点击输入框及选择图片和发送按钮时软键盘显示且不消失,点击其他区域,则隐藏软键盘。
主要代码如下:
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
if (ev!!.getAction() === MotionEvent.ACTION_DOWN) {
val v = currentFocus
if (isShouldHideInput(v, ev!!)) {//点击的是其他区域,则调用系统方法隐藏软键盘
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (imm != null) {
imm!!.hideSoftInputFromWindow(v!!.windowToken, 0)
}
}
return super.dispatchTouchEvent(ev)
}
// 必不可少,否则所有的组件都不会有TouchEvent了
return if (window.superDispatchTouchEvent(ev)) {
true
} else onTouchEvent(ev)
}
/**
* 判断是否是输入框区域
*/
fun isShouldHideInput(v: View?, event: MotionEvent): Boolean {
if (v !=