使用申报设置样式设置自定义组件的输入型自定义、样式、组件

2023-09-05 11:17:41 作者:苦笑流年记忆

我有一个CompositeComponent(的EditText +的ImageButton) 当按钮点击的EditText的内容将被清除。 这是工作的罚款。我的问题是设置属性为我的组件。我使用的申报,设置样式,设置属性为我的组件。

I have a CompositeComponent (EditText+ImageButton) When clicking on button the edittext content will be cleared. It is working fine. My problem is setting attributes to my component. I am using declare-styleable to set attributes to my component.

我成功地设置的minlines,MAXLINES和文字颜色。

I am successful at setting minLines, maxLines and textColor.

如何设置inputtype通过XML我的部分。

How can I set inputtype to my component via xml.

我attributes.xml

my 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>

和的myComponent的在main_layout.xml用法:

And usage of mycomponent in main_layout.xml:

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

普通EDITTEXT用法:

Ordinary Edittext usage:

<EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberSigned" <--------I want to use this property--------> >

我不能在我的attribute.xml使用ENUM。 如何参照安卓inputType =numberSigned在我的 CET:InputType

I cant use ENUM in my attribute.xml. How to refer android:inputType="numberSigned" in my cet:InputType?

编辑:

这是我在我的ClearableEditText.java分配属性

This is how I assign properties in my ClearableEditText.java

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

            int minLines = a.getInt(R.styleable.CET_MinLines, 1);
            int maxLines = a.getInt(R.styleable.CET_MaxLines, 100);
            String hint = a.getString(R.styleable.CET_Hint);
            int textColor = a.getColor(R.styleable.CET_TextColor, Color.BLACK);
            int inputType = a.getInt(R.styleable.CET_InputType, -108);

            Log.i(TAG, "ClearableEditText: Min Line "+minLines +" Max Lines: "+maxLines+" Hint "+hint+" Color: "+textColor+" Input Type: "+inputType);

            edit_text.setMaxLines(maxLines);
            edit_text.setMinLines(minLines);
            edit_text.setTextColor(textColor);
            edit_text.setHint(hint);
            if(inputType != -108)
                edit_text.setInputType(inputType);

您可以看到有一个与inputType财产分配给EDITTEXT没有问题。

You can see there is no problem with assigning the inputType property to editText.

推荐答案

我设法生int值做到这一点: 我觉得这是不好的做法。 我可以这样分配的原始值: CET:InputType =2

I managed to do it with raw int value: I think it is not good practice. I can assign raw values like this: cet:InputType="2"

2号(简称为值:http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_NUMBER和的http://developer.android.com/reference/android/R.styleable.html#TextView_inputType)

2 for number (Referred for values: http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_NUMBER and http://developer.android.com/reference/android/R.styleable.html#TextView_inputType)

我相信&LT; attr指示NAME =InputType格式=参考/&GT; 可以帮助,但不知道如何使用它

I believe <attr name="InputType" format="reference" /> can help but don't know how to use it.