相关疑难解决方法(0)

使用declare styleable设置自定义组件输入类型

我有一个CompositeComponent(EditText + ImageButton)当点击按钮时,将清除edittext内容.它工作正常.我的问题是为我的组件设置属性.我使用declare-styleable来设置我的组件的属性.

我成功设置了minLines,maxLines和textColor.

如何通过xml将inputtype设置为我的组件.

我的attributes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CET">
        <attr name="MaxLines" format="integer"/>
        <attr name="MinLines" format="integer"/>
        <attr name="TextColor" format="color"/>
        <attr name="InputType" format="integer" />
        <attr name="Hint" format="string" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

并在main_layout.xml中使用mycomponent:

<com.test.ui.ClearableEditText
        xmlns:cet="http://schemas.android.com/apk/res/com.test.ui"
        android:id="@+id/clearableEditText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        cet:MaxLines="2"
        cet:MinLines="1"
        cet:TextColor="#0000FF"
        cet:InputType="" <---I cant set this property--------->
        cet:Hint="Clearable EditText Hint">

    </com.test.ui.ClearableEditText>
Run Code Online (Sandbox Code Playgroud)

普通的Edittext用法:

<EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberSigned" <--------I want to use this property--------> >
Run Code Online (Sandbox Code Playgroud)

我不能在我的attribute.xml中使用ENUM.怎么引用android:inputType="numberSigned"我的 cet:InputType

编辑:

这就是我在ClearableEditText.java中分配属性的方法

TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.CET,0, 0);

            int minLines = a.getInt(R.styleable.CET_MinLines, 1); …
Run Code Online (Sandbox Code Playgroud)

android custom-component attr

35
推荐指数
2
解决办法
3万
查看次数

标签 统计

android ×1

attr ×1

custom-component ×1