关闭/隐藏kotlin中的SoftinputKeyboard

我有一个按钮和edittext。 当用户在edittext中完成输入并按下按钮时,我想关闭我的软键盘。

或者任何指南或参考链接。

调用这个函数来隐藏系统键盘:

fun View.hideKeyboard() { val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(windowToken, 0) } 
 fun hideSoftKeyboard(mActivity: Activity) { // Check if no view has focus: val view = mActivity.currentFocus if (view != null) { val inputManager = mActivity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputManager.hideSoftInputFromWindow(view.windowToken, 0) } } fun showKeyboard(yourEditText: EditText, activity: Activity) { try { val input = activity .getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager input.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT) } catch (e: Exception) { e.printStackTrace() } } 

我稍微修改@Serj Ardovic的回应

 private fun hideKeyboard(view: View) { view?.apply { val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(view.windowToken, 0) } } 

因为它真的符合我的要求