如何以编程方式在视图上设置自定义属性

时间:2018-02-05 10:41:26

标签: java android android-custom-view

我尝试以编程方式将自定义属性设置为我的视图,以设置验证状态。

首先,我为文本字段创建了此布局:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item>
    <shape>
        <solid android:color="@android:color/transparent" />
    </shape>
</item>

<item android:right="1dp" android:left="1dp">
    <shape>
        <solid android:color="@color/state_color" />
    </shape>
</item>

<!-- main color -->
<item
    android:bottom="1.5dp"
    android:left="2.5dp"
    android:right="2.5dp">
    <shape>
        <solid android:color="@android:color/white" />
    </shape>
</item>

<!-- draw another block to cut-off the left and right bars -->
<item android:bottom="5.0dp">
    <shape>
        <solid android:color="@android:color/white" />
    </shape>
</item>

然后定义了这个属性:

<attr name="validation_state" format="enum">
        <enum name="None" value="0"/>
        <enum name="Error" value="1"/>
        <enum name="Warning" value="2"/>
        <enum name="Success" value="3"/>
</attr>

我还定义了这些颜色定义state_color.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:color="@color/state_error"
    app:validation_state="Error"/>

<item
    android:color="@color/state_warning"
    app:validation_state="Warning"/>

<item
    android:color="@color/state_success"
    app:validation_state="Success"/>

<item
    android:color="@color/state_default"/>

最后我创建了一个自己的EditText类,它扩展了Android框架之一

    private static final int[] STATE_MANDATORY = { R.attr.mandatory };
private static final int[] STATE_READONLY = { R.attr.readonly };
private static final int[] STATE_VALIDATION_STATE = { R.attr.validation_state };

private boolean _mandatory = false;
private boolean _readonly = false;
private ValidationState _validationState = ValidationState.NONE;

public mcEditText(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

@Override
public void setMandatory(final boolean mandatory)
{
    _mandatory = mandatory;
    refreshDrawableState();
}

@Override
public void setReadOnly(final boolean readonly)
{
    _readonly = readonly;
    refreshDrawableState();
}

@Override
public void setValidationState(final ValidationState state)
{
    _validationState = state;
    refreshDrawableState();
}

@Override
protected int[] onCreateDrawableState(int extraSpace)
{
    final int[] drawableState = super.onCreateDrawableState(extraSpace + 3);
    if (_mandatory)
    {
        mergeDrawableStates(drawableState, STATE_MANDATORY);
    }
    if (_readonly)
    {
        mergeDrawableStates(drawableState, STATE_READONLY);
    }
    if (_validationState != ValidationState.NONE)
    {
        mergeDrawableStates(drawableState, STATE_VALIDATION_STATE);
    }
    return drawableState;

}

我已将此颜色定义添加到我的视图中,并且还识别它,因此会显示错误状态。但现在我不知道如何在不同的州之间切换。我需要以编程方式设置属性&#34; validation_state&#34;,但这怎么可能?

1 个答案:

答案 0 :(得分:1)

似乎android不支持这类工作的枚举属性。您必须使用布尔属性

<attr name="validation_state_none" format="boolean"/>
<attr name="validation_state_error" format="boolean"/>
...

您尝试执行的操作的最常见示例是可聚焦的可编辑属性。它由android source

中的原子布尔属性处理

可能对您有帮助的另一种解决方案(未经测试)是以编程方式创建自己的drawable。请查看DrawableContainerStateListDrawable