【问题标题】:Reusing 'inputType' attribute from TextView in custom view在自定义视图中重用 TextView 中的“inputType”属性
【发布时间】:2017-07-28 12:23:57
【问题描述】:

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

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

我已经在https://stackoverflow.com/a/10361617/313042提到了解决方案

attr 文件包含:

<declare-styleable name="MyEditText">
    <attr name="android:inputType"/>
</declare-styleable>

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;

    }
}

布局文件包含:

<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" />

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

【问题讨论】:

  • 你只需要声明新的属性
  • @NickCardoso ,你的意思是我不应该声明:

标签: android android-edittext textview android-custom-view android-attributes


【解决方案1】:

您的拼写错误textMultiline 必须是textMultiLine

请通过this link中的xml查看支持的android:inputType

【讨论】:

  • 效果很好。谢谢你。想出这个可能需要很长时间。
  • 是的。有时,我想如果 IDE 可以支持我们检测错字(开个玩笑),那该多好。
  • 好吧,这不是一个坏主意 - Google 可以为此引入警告 - 例如“您的意思是 textMultiLine 吗?”内置于 Android Studio 中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多