私はボタンとedittextを持っています。ユーザーがedittextで入力を完了してボタンを押すと、私はソフトキーボードを閉じたいと思う。kotlinのSoftinputKeyboardを閉じる/隠す
またはガイドラインまたは参照リンク。
私はボタンとedittextを持っています。ユーザーがedittextで入力を完了してボタンを押すと、私はソフトキーボードを閉じたいと思う。kotlinのSoftinputKeyboardを閉じる/隠す
またはガイドラインまたは参照リンク。
私は少し@Serj Ardovic
private fun hideKeyboard(view: View) {
view?.apply {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}
の応答を変更
コールシステムキーボードを非表示にするには、この関数は:そのは本当に私の要件のために働くよう
fun View.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
お返事をありがとうございます。わたしにはできる。 –
@ RushabhShah - 答えがあなたを助けた場合は、 "正解"とマークすることを確認してください –
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()
}
}
ますすべての機能を拡張することができますEditText
は、常にフォーカスのときにソフトキーボードを隠すことになりますEditText
が失われます。あなたが非表示のキーボードをしたい場合は、いくつかのEditText
ためのフォーカスが失われたとき、ちょうど私達はちょうど追加したり、ここで私たち自身のOnFocusLostListener
fun EditText.hideSoftKeyboardOnFocusLostEnabled(enabled: Boolean) {
val listener = if (enabled)
OnFocusLostListener()
else
null
onFocusChangeListener = listener
}
を削除EditText
ための拡張では、このEditText
editText.hideSoftKeyboardOnFocusLostEnabled(true)
のためのコード行を使用付属のView
のフォーカスを失った場合にキーボードを隠すOnFocusLostListener
の実装です。
class OnFocusLostListener: View.OnFocusChangeListener {
override fun onFocusChange(v: View, hasFocus: Boolean) {
if (!hasFocus) {
val imm = v.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(v.windowToken, 0)
}
}
}
[Kotlinと閉じる/非表示Androidのソフトキーボード]の
可能な重複(https://stackoverflow.com/questions/41790357/close-hide-the-android-soft-keyboard-with-kotlin) – R2R