我正在尝试用Kotlin编写一个简单的Android应用程序,我的布局中有一个EditText和一个Button。在编辑字段中输入内容并点击按钮后,我想隐藏虚拟键盘。
这里有一个关于Java如何实现的热门问题关闭/隐藏Android软键盘,但据我所知,Kotlin应该有另一种替代版本。我该怎么做呢?
我正在尝试用Kotlin编写一个简单的Android应用程序,我的布局中有一个EditText和一个Button。在编辑字段中输入内容并点击按钮后,我想隐藏虚拟键盘。
这里有一个关于Java如何实现的热门问题关闭/隐藏Android软键盘,但据我所知,Kotlin应该有另一种替代版本。我该怎么做呢?
我在这里找到了适合我的答案:http://programminget.blogspot.com/2017/08/how-to-close-android-soft-keyboard.html
该链接提供了有关如何关闭安卓软键盘的解决方法。val inputManager:InputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(currentFocus.windowToken, InputMethodManager.SHOW_FORCED)
private fun hideKeyboard(){
// since our app extends AppCompatActivity, it has access to context
val imm=getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
// we have to tell hide the keyboard from what. inorder to do is we have to pass window token
// all of our views,like message, name, button have access to same window token. since u have button
imm.hideSoftInputFromWindow(button.windowToken, 0)
// if you are using binding object
// imm.hideSoftInputFromWindow(binding.button.windowToken,0)
}
您需要在需要的地方调用此函数。
这在API 26版本中可以很好地运行。
val view: View = if (currentFocus == null) View(this) else currentFocus
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
你好,我经常使用这两个扩展函数来显示和隐藏软键盘。
显示软键盘
fun Any.showSoftKeyboard(view: View, context: Context) {
if (view.requestFocus()) {
val imm: InputMethodManager =
context.getSystemService(Context.INPUT_METHOD_SERVICE) as
InputMethodManager
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
}
隐藏软键盘
fun Any.hideSoftKeyboard(view: View, context: Context) {
val imm =
context.getSystemService(Context.INPUT_METHOD_SERVICE) as
InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
currentFocus?.let { currFocus ->
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(currFocus.windowToken, 0)
}
return super.dispatchTouchEvent(ev)
}
你可以使用下面的代码,在我的片段中我写了下面的代码:
private val myLayout = ViewTreeObserver.OnGlobalLayoutListener {
yourTextView.isCursorVisible = KeyboardTool.isSoftKeyboardShown(myRelativeLayout.rootView)
}
然后在fragment
的onViewCreated
中:
......
super.onViewCreated(view, savedInstanceState)
myRelativeLayout.viewTreeObserver.addOnGlobalLayoutListener(myLayout)
......
在 onDestroyView
中也要使用:
override fun onDestroyView() {
super.onDestroyView()
myRelativeLayout.viewTreeObserver.removeOnGlobalLayoutListener(myLayout)
}
并且:
object KeyboardTool {
fun isSoftKeyboardShown(rootView: View): Boolean {
val softKeyboardHeight = 100
val rect = Rect()
rootView.getWindowVisibleDisplayFrame(rect)
val dm = rootView.resources.displayMetrics
val heightDiff = rootView.bottom - rect.bottom
return heightDiff > softKeyboardHeight * dm.density
}
}
editText.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->
if (!hasFocus) {
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
val focusedView = currentFocus
if (imm != null && focusedView != null) {
imm.hideSoftInputFromWindow(focusedView.windowToken, 0)
}
}
}
fun Activity.hideKeyboard() {
hideKeyboard(currentFocus ?: View(this))
}