【问题标题】:Android EditText inputType="none" doesn't work, becomes "textMultiLine"Android EditText inputType="none" 不起作用,变成“textMultiLine”
【发布时间】:2012-04-29 08:47:33
【问题描述】:

在 Android 1.6(4) 和 2.3.3(10) 上测试。

我制作了一个简约的测试应用程序来演示这一点,它所做的只是加载 xml:

setContentView(R.layout.main); 

xml 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="none"
    android:ems="10" >

</EditText>

问题:

当设置inputType="none"在执行过程中实际输入类型变成textMultiLine(0x00020001)时,我已经用调试器检查过了。

另一方面,如果我使用inputType="text",它会按预期工作。

这是 Android 中的错误吗?

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    我遇到了同样的问题:通过 xml 定义输入类型不起作用。

    然后,为了修复它,我以编程方式设置输入类型:

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
        {
        ...
        editText1= (EditText) super.getActivity().findViewById(R.id.editText1);
        editText1.setInputType(InputType.TYPE_NULL);    // none...
        ...
        }
    

    这对我有用。

    【讨论】:

    • 更好用setInputType(InputType.TYPE_NULL)
    • 这行得通,但为了让点击监听器触发我必须点击两次。想法?我已经尝试将 focusable 和 focusableInTouchMode 设置为 false
    【解决方案2】:

    设置 InputType="none" xml 不起作用,但如果您使用数据绑定,那么您可以使用绑定语法来设置输入类型。

    导入类型..

    <data> <import type="android.text.InputType" /> </data>

    然后绑定值

    <EditText android:id="@+id/edit_text" android:inputType="@{InputType.TYPE_NULL}" />

    【讨论】:

    • 使用材料库的TextInputEditText,将inputType 设置为noneeditable 设置为false 没有帮助,但这工作正常。第一个答案是在 Java/Kotlin 中执行此操作。
    • 这行得通,但为了让点击监听器触发我必须点击两次。想法?我已经尝试将 focusable 和 focusableInTouchMode 设置为 false
    【解决方案3】:

    据我所知,这是 android 代码中的一个错误。请参阅此内容 - How to make EditText not editable through XML in Android?

    GalDude33 建议-

        android:clickable="false" 
        android:cursorVisible="false" 
        android:focusable="false" 
        android:focusableInTouchMode="false"
    

    【讨论】:

      【解决方案4】:

      使用android:editable="false"。尽管它已被弃用,但它在 inputType 不被弃用时仍然有效。

      【讨论】:

      • 是的,你是真的。这正是正确的作品。因为 inputType="none" 是废话更新。
      • 其实这是正确的答案,因为 EditText 默认是 editable = true 并且 inputType 在这种情况下被更改为 TEXT ,在这种情况下它不关心“none”,因为这也是默认的价值。请参阅 TextView 的代码 `` else if (inputType != EditorInfo.TYPE_NULL) { ... } ... else if (editable) { createEditorIfNeeded(); mEditor.mKeyListener = TextKeyListener.getInstance(); mEditor.mInputType = EditorInfo.TYPE_CLASS_TEXT; } ```
      • @azwar_akbar 是的,你是对的。 inputType="none" 对任何人都不起作用。您必须使用 InputType.TYPE_NULL 在代码中以编程方式设置它,或者使用 xml 中已弃用的 android:editable="false" 属性
      【解决方案5】:

      使用这个

       textView.setKeyListener(null);
      

      【讨论】:

      • 您先生,是我的英雄。我尝试了上面所有其他建议的解决方案以及其他一些解决方案,但这是一件很美的事情。除此以外,所有其他方法都需要权衡取舍。
      • 如何重新启用?
      【解决方案6】:

      像这样设置 .xml 是可行的,但是,我不得不添加另一行,因为光标是可见的。

      android:focusable="false"
      android:cursorVisible="false" 
      

      奇怪的是,即使在 2 年后仍然存在这个“错误”,而且折旧的方法比建议的方法效果更好。

      使用它也可以,但在这种情况下您必须编辑背景颜色,除非您希望颜色不同。

      android:enabled="false"
      

      【讨论】:

        【解决方案7】:

        以下代码有效:

        使用android:enabled="false"

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-03-17
          • 2012-07-30
          • 2019-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-01
          相关资源
          最近更新 更多