软键盘隐藏
2023-10-31 本文已影响0人
河马过河
工具类
object KeyBoardUtils {
fun showKeyboard(view: View?) {
view?.context?.let {
val inputMethodManager =
it.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(view, 0)
}
}
fun hideKeyboard(view: View?){
view?.context?.let {
val inputMethodManager =
it.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.RESULT_UNCHANGED_SHOWN)
}
}
/**
* 关闭软键盘
*/
private fun hideKeyboard(activity:Activity?) {
activity?.let {
val imm = it.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if(imm.isActive && it.currentFocus !=null){
if (it.currentFocus?.windowToken !=null) {
imm.hideSoftInputFromWindow(it.currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
}
}
2 调用时机
override fun finish() {
super.finish()
KeyBoardUtils.hideKeyboard(etPassword)
}