在自定义视图中重用 TextView 中的“inputType”属性

raj*_*ath 5 android textview android-custom-view android-edittext android-attributes

我试图在自定义视图中重用 TextView 中的“android:inputType”属性,但得到的错误是:

不允许使用字符串类型(在“inputType”处,值为“textMultiline”)。

我已参考/sf/answers/725313221/的解决方案

attr 文件包含:

<declare-styleable name="MyEditText">
    <attr name="android:inputType"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

MyEditText.java 是:

int n = typedArray.getIndexCount();
for (int i = 0; i < n; i++) {
    int attr = typedArray.getIndex(i);

    switch (attr) {
        case R.styleable.MyEditText_android_inputType:
            inputTypes = typedArray.getInt(attr, EditorInfo.TYPE_NULL);
            break;

    }
}
Run Code Online (Sandbox Code Playgroud)

布局文件包含:

<com.example.MyEditText
  android:id="@+id/met"
  style="@style/MyStyle"
  android:layout_marginLeft="0dp"
  android:layout_marginRight="0dp"
  android:layout_marginTop="12dp"
  android:inputType="textMultiline" />
Run Code Online (Sandbox Code Playgroud)

我有什么办法可以解决这个问题吗?谢谢。

Nam*_*mNH 4

你的错字textMultiline一定是textMultiLine

请参阅此链接android:inputType中的通过 xml支持

  • 好吧,这并不是一个坏主意 - Google 可以为此引入警告 - 类似于“您是说 textMultiLine 吗?” 内置于 Android Studio 中。 (2认同)