使用Kotlin关闭/隐藏Android软键盘

Eug*_*nov 34 android android-softkeyboard kotlin

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

有一个流行的问题 关闭/隐藏Android软键盘关于在Java中执行它,但据我所知,应该有一个替代版本的Kotlin.我该怎么办?

Pét*_*űcs 42

我想我们可以稍微提高Viktor的答案.基于它始终附加到视图,将有上下文,如果有上下文,则有InputMethodManager

fun View.hideKeyboard() {
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(windowToken, 0)
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,上下文自动表示视图的上下文.你怎么看?


Gun*_*han 34

在"活动","片段"中使用以下实用程序功能隐藏软键盘.

(*)最新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)
}
Run Code Online (Sandbox Code Playgroud)

老答案:

fun Fragment.hideKeyboard() {
    activity.hideKeyboard(view)
}

fun Activity.hideKeyboard() {
    hideKeyboard(if (currentFocus == null) View(this) else currentFocus)
}

fun Context.hideKeyboard(view: View) {
    val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
Run Code Online (Sandbox Code Playgroud)

这将关闭键盘,无论您的代码如何在对话框片段和/或活动等.

活动/片段中的用法:

hideKeyboard()
Run Code Online (Sandbox Code Playgroud)

  • @Dan 我将这些函数保存在我的 ContextExtensions.kt 文件中,但您可以将它们保存在您认为合适的任何地方。 (3认同)
  • 天哪,非常感谢。来自iOS,这对我来说似乎很荒谬,这甚至是一个问题,但你的解决方案是我见过的最干净的。谢谢你! (2认同)

小智 9

只需在您的活动中覆盖此方法即可。它也将自动在其子片段中工作.....

在JAVA中

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (getCurrentFocus() != null) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
    return super.dispatchTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)

在科特林

override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (currentFocus != null) {
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
        }
        return super.dispatchTouchEvent(ev)
    }
Run Code Online (Sandbox Code Playgroud)

如果它适合您,请投票...谢谢.....


MeL*_*ine 8

我没有看到 Kotlin 扩展函数的这个变体:

fun View.hideSoftInput() {
    val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
Run Code Online (Sandbox Code Playgroud)

它的好处是可以从每个 CustomView 以及每个点击或触摸侦听器中调用此扩展函数


小智 6

创建一个名为 Utils 的对象类:

object Utils {

    fun hideSoftKeyBoard(context: Context, view: View) {
        try {
            val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm?.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
        } catch (e: Exception) {
            // TODO: handle exception
            e.printStackTrace()
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在任何要隐藏软输入键盘的类中使用此方法。我在我的中使用这个BaseActivity.

这里的视图是您在布局中使用的任何视图:

Utils.hideSoftKeyBoard(this@BaseActivity, view )
Run Code Online (Sandbox Code Playgroud)


Vik*_*nin 5

您可以使用 Anko 使生活更轻松,因此该行将是:

inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
Run Code Online (Sandbox Code Playgroud)

或者更好地创建扩展功能:

fun View.hideKeyboard(inputMethodManager: InputMethodManager) {
    inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
Run Code Online (Sandbox Code Playgroud)

并这样称呼它:

view?.hideKeyboard(activity.inputMethodManager)
Run Code Online (Sandbox Code Playgroud)


mic*_*l.z 5

Peter的解决方案通过扩展View类的功能来巧妙地解决了该问题。另一种方法是扩展Activity类的功能,从而将隐藏键盘的操作与View的容器而不是View本身绑定。

fun Activity.hideKeyboard() {
    val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(findViewById(android.R.id.content).getWindowToken(), 0);
}
Run Code Online (Sandbox Code Playgroud)


Sed*_*ush 5

在您的 Activity 或 Fragment 中创建一个函数:

fun View.hideKeyboard() {
 val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
      inputManager.hideSoftInputFromWindow(windowToken, 0)
}
Run Code Online (Sandbox Code Playgroud)

假设您your_button_id在与此活动或片段相关的 XML 文件中有一个带有 id 的按钮,因此,在按钮单击事件中:

    your_button_id.setOnClickListener{
       it.hideKeyboard()
     }
Run Code Online (Sandbox Code Playgroud)


MRa*_*iaz 5

虽然答案有很多,但这个答案与KOTLIN中的一个最佳实践有关,即打开和关闭具有生命周期和扩展功能的键盘。

1)。创建扩展函数创建文件EditTextExtension.kt并粘贴以下代码

fun EditText.showKeyboard(
 ) {
  requestFocus()
  val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as 
  InputMethodManager
  imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
 }

fun EditText.hideKeyboard(
) {
 val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as 
 InputMethodManager
 imm.hideSoftInputFromWindow(this.windowToken, 0)
 }
Run Code Online (Sandbox Code Playgroud)

2)。创建 LifeCycleObserver 类创建一个类EditTextKeyboardLifecycleObserver.kt并粘贴以下代码

class EditTextKeyboardLifecycleObserver(
 private val editText: WeakReference<EditText>
 ) :
 LifecycleObserver {

 @OnLifecycleEvent(
     Lifecycle.Event.ON_RESUME
 )
 fun openKeyboard() {
     editText.get()?.postDelayed({ editText.get()?.showKeyboard() }, 50)
 }
 fun hideKeyboard() {
     editText.get()?.postDelayed({ editText.get()?.hideKeyboard() }, 50)
 }
}
Run Code Online (Sandbox Code Playgroud)

3)。然后在onViewCreated / onCreateView中使用以下代码

lifecycle.addObserver(
         EditTextKeyboardLifecycleObserver(
             WeakReference(mEditText) //mEditText is the object(EditText)
         )
     )
Run Code Online (Sandbox Code Playgroud)

当用户打开片段或活动时,键盘将打开。

如果您遇到任何问题,请按照解决方案随时在评论中提问。