美文网首页
软键盘隐藏

软键盘隐藏

作者: 河马过河 | 来源:发表于2023-10-31 11:13 被阅读0次

    工具类

    object KeyBoardUtils {
        fun showKeyboard(view: View?) {
            view?.context?.let {
                val inputMethodManager =
                    it.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                inputMethodManager.showSoftInput(view, 0)
            }
    
        }
        fun hideKeyboard(view: View?){
            view?.context?.let {
                val inputMethodManager =
                    it.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                inputMethodManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.RESULT_UNCHANGED_SHOWN)
            }
        }
        /**
         * 关闭软键盘
         */
        private fun  hideKeyboard(activity:Activity?) {
            activity?.let {
                val imm = it.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                if(imm.isActive && it.currentFocus !=null){
                    if (it.currentFocus?.windowToken !=null) {
                        imm.hideSoftInputFromWindow(it.currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
            }
    
        }
    }
    

    2 调用时机

      override fun finish() {
            super.finish()
            KeyBoardUtils.hideKeyboard(etPassword)
        }
    

    注意事项:在ondestory 中调用,不起作用

    相关文章

      网友评论

          本文标题:软键盘隐藏

          本文链接:https://www.haomeiwen.com/subject/wvbiidtx.html