使用Kotlin关闭/隐藏Android软键盘 [英] Close/hide the Android Soft Keyboard with Kotlin

查看:63
本文介绍了使用Kotlin关闭/隐藏Android软键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to write a simple Android app in Kotlin. I have an EditText and a Button in my layout. After writing in the edit field and clicking on the Button, I want to hide the virtual keyboard.

有一个热门问题 关闭/隐藏Android软键盘关于在Java中进行操作的信息,但据我了解,应该有Kotlin的替代版本.我该怎么办?

There is a popular question Close/hide the Android Soft Keyboard about doing it in Java, but as far as I understand, there should be an alternative version for Kotlin. How should I do it?

推荐答案

我认为我们可以稍微改善Viktor的答案.基于它始终附加到视图的基础上,将存在上下文,如果存在上下文,则存在InputMethodManager

I think we can improve Viktor's answer a little. Based on it's always attached to a view, there will be context, if there is context then there is InputMethodManager

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

在这种情况下,上下文自动表示视图的上下文. 你觉得呢?

In this case the context automatically means the context of the view. What do you think?

这篇关于使用Kotlin关闭/隐藏Android软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆