EditText:抑制软键盘但允许选择和光标?

EditText: suppress soft keyboard but allow selection and cursor?

我想显示一个 EditText,主要通过选择它的一部分并按按钮插入新值来编辑它。 (这些值是我不希望用户必须键入的长十六进制字符串。)因此,我想抑制软键盘的显示,除非用户故意调用它。

This question describes almost exactly the same requirement, but the accepted answer 不适用于 Android 5.1。当输入模式更改为 InputType.TYPE_NULL 时,EditText 禁用 所有 编辑功能,而不仅仅是软键盘。没有光标,没有点击拖动选择等

按照 this answer 中的建议覆盖 onCheckIsTextEditor() 的行为方式相同。

有什么方法可以在 Android 5 上完成这个吗?

这是在 API 21 中添加的:

EditText editText = (EditText) findViewById(R.id.edittext);
editText.setShowSoftInputOnFocus(false);

根据grepcode,此方法自4.1.1版本以来一直存在但被隐藏。所以你可以将调用它的方法注释为@TargetApi(Build.VERSION_CODES.LOLLIPOP),并用if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN).

保护调用本身