简体   繁体   English

单击EditText时如何禁用键盘?

[英]How to disable the keyboard when I click on EditText?

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. 您好,我想做下一件事:当我单击EditText时,我想隐藏键盘但看到光标。 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. 显然,我看不到键盘,但无法单击EditText。 How can I do this ? 我怎样才能做到这一点 ? To be precise I am using Kotlin. 确切地说,我正在使用Kotlin。

Thank you ! 谢谢 !

If you have minimum API >= 21 : 如果您的最低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
}

Set a listener for your EditText's onFocus, you can add: 为您的EditText的onFocus设置一个侦听器,您可以添加:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

How to avoid automatically appear android keyboard when activity start 如何避免活动开始时自动出现android键盘

inside the listener method force the android to hide the virtual keyboard using the hideSoftInputFromWindow method in the InputMethodManager 侦听器方法内部强制android使用InputMethodManager中hideSoftInputFromWindow方法隐藏虚拟键盘

View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

try this in manifest file 在清单文件中尝试

:
<activity
...
android:windowSoftInputMode="stateHidden|adjustResize"
...
>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM