【问题标题】:EditText: suppress soft keyboard but allow selection and cursor?EditText:抑制软键盘但允许选择和光标?
【发布时间】:2015-06-25 05:21:40
【问题描述】:

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

This question 描述了几乎完全相同的要求,但 accepted answer 不适用于 Android 5.1。当输入模式更改为InputType.TYPE_NULL 时,EditText 将禁用所有 编辑功能,而不仅仅是软键盘。没有光标,没有点击拖动选择等。

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

有什么方法可以在 Android 5 上实现这一点吗?

【问题讨论】:

    标签: android android-edittext android-softkeyboard


    【解决方案1】:

    这是在 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) 保护调用本身。

    【讨论】:

    • 效果很好。 When part of the text is selected, a drawer-like view opens from the top of the screen with "select all", "cut", and "paste" buttons.我可以忍受。
    • 我编辑了答案以解释它适用于较低 API 的真正原因。它与支持库无关。
    猜你喜欢
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 2014-12-13
    • 2016-03-02
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多