问题
Hello I would like to do the next thing : when I click on an EditText I would like to hide the keyboard but seeing the cursor. I tried to do this :
editText_test!!.setCursorVisible(false);
editText_test!!.setFocusableInTouchMode(false);
editText_test!!.setFocusable(true);
Obviously I don't see the keyboard but I can't click on my EditText. How can I do this ? To be precise I am using Kotlin.
Thank you !
回答1:
If you have minimum API >= 21:
editText_test!!.showSoftInputOnFocus = false
To deal with different versions:
if (Build.VERSION.SDK_INT >= 21) {
editText_test!!.showSoftInputOnFocus = false
} else if (Build.VERSION.SDK_INT >= 11) {
editText_test!!.setRawInputType(InputType.TYPE_CLASS_TEXT)
editText_test!!.setTextIsSelectable(true)
} else {
editText_test!!.setRawInputType(InputType.TYPE_NULL)
editText_test!!.isFocusable = true
}
回答2:
Set a listener for your EditText's onFocus, you can add:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
How to avoid automatically appear android keyboard when activity start
回答3:
inside the listener method force the android to hide the virtual keyboard using the hideSoftInputFromWindow method in the InputMethodManager
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
回答4:
try this in manifest file
:
<activity
...
android:windowSoftInputMode="stateHidden|adjustResize"
...
>
来源:https://stackoverflow.com/questions/45557197/how-to-disable-the-keyboard-when-i-click-on-edittext