关闭/隐藏Android软键盘(Kotlin实现)

112

我正在尝试用Kotlin编写一个简单的Android应用程序,我的布局中有一个EditText和一个Button。在编辑字段中输入内容并点击按钮后,我想隐藏虚拟键盘。

这里有一个关于Java如何实现的热门问题关闭/隐藏Android软键盘,但据我所知,Kotlin应该有另一种替代版本。我该怎么做呢?

19个回答

4

2
编写一个隐藏键盘的函数:
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)

    }

您需要在需要的地方调用此函数。


2

这在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)

1

你好,我经常使用这两个扩展函数来显示和隐藏软键盘。

  1. 显示软键盘

    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)
       }
    }
    
  2. 隐藏软键盘

    fun Any.hideSoftKeyboard(view: View, context: Context) {
          val imm =
          context.getSystemService(Context.INPUT_METHOD_SERVICE) as 
    InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }
    
您可以在任何Object类中使用这些方法以进行全局访问,或者您可以创建单独的Extensions/CommonUtils文件来使用它们。

1
由于“currentFocus”是可空的,我们最好使用Kotlin的?.let检查它。感谢@Zeeshan Ayaz,这里是稍微改进版。
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)
}

0
Kotlin 我使用以下代码:
import splitties.systemservices.inputMethodManager inputMethodManager.hideSoftInputFromWindow(view?.windowToken, 0)

0

你可以使用下面的代码,在我的片段中我写了下面的代码:

private val myLayout = ViewTreeObserver.OnGlobalLayoutListener {
    yourTextView.isCursorVisible = KeyboardTool.isSoftKeyboardShown(myRelativeLayout.rootView)
}

然后在fragmentonViewCreated中:

......
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
    }
}

0
你可以简单地为你的视图添加OnFocusChangeListener。例如,在这种情况下是editText。
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)
           }
        }
    }

0
你可以在 Kotlin 中使用函数扩展。如果需要在 Fragment 中实现,请将 activity 替换为 fragment。
fun Activity.hideKeyboard() {
    hideKeyboard(currentFocus ?: View(this))
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接