首页 文章

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

提问于
浏览
7

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

关于在Java中做这件事有一个 popular question Close/hide the Android Soft Keyboard,但就我所知,Kotlin应该有一个替代版本 . 我该怎么办?

7 回答

  • 0

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

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

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

  • 1

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

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

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

    Usage in Activity/Fragment:

    hideKeyboard()
    
  • 1

    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);
    }
    
  • 20

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

    inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
    

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

    fun View.hideKeyboard(inputMethodManager: InputMethodManager) {
        inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
    }
    

    并称之为:

    view?.hideKeyboard(activity.inputMethodManager)
    
  • 4

    这适用于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

    我在这里找到了对我有用的答案: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)
    
  • 11

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

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

    然后在 onViewCreatedfragment

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

相关问题