Используйте объявление стиля для установки типа ввода пользовательского компонента

У меня есть CompositeComponent (EditText+ImageButton) При нажатии на кнопку содержимое edittext будет очищено. Он работает нормально. Моя проблема заключается в установке атрибутов для моего компонента. Я использую declare-styleable для установки атрибутов моего компонента.

Мне удалось установить minLines, maxLines и textColor.

Как установить тип ввода для моего компонента через xml.

мой attribute.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:

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

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

Я не могу использовать ENUM в своем attribute.xml.Как указать 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);
            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.

35
задан Mahendran 16 March 2012 в 11:32
поделиться