我正在使用kotlin并制作平板电脑应用,因为我想手动关闭键盘的片段,但是我无法做到这一点,当edittext聚焦时键盘也不会出现
在“活动,片段”中使用以下实用程序功能来隐藏软键盘。
对于最新的Kotlin版本
fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) }
}
fun Activity.hideKeyboard() {
hideKeyboard(currentFocus ?: View(this))
}
fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
这将关闭键盘,而不管对话框片段和/或活动等中的代码如何。
活动/片段中的用法:
hideKeyboard()