微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在我的自定义视图中重用一个标准的android属性

我正在创建一个具有以下布局的自定义复合视图
<merge xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="horizontal">

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:singleLine="true"/>

</merge>

你可以看到,它只是一个TextView和一个EditText.我希望能够提供属性到我的自定义视图,转发到TextView或EditText.例如

<codeguru.labelededittext.LabeledEditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:label="@string/label"
    app:hint="@string/hint"/>

我已经弄清楚如何将这些字符串属性转发到TextView和EditText,代表性地:

TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.LabeledEditText,0);

    try {
        label.setText(a.getString(R.styleable.LabeledEditText_label));
        edit.setHint(a.getString(R.styleable.LabeledEditText_hint));
    } finally {
        a.recycle();
    }

现在我也想设置EditText的inputType.如果我创建一个< attr name =“inputType”format =“flag”>标签,我必须填充所有可能的标志值吗?有没有办法重用已经由EditText声明的值?

解决方法

你可以得到这个:
int[] values = new int[]{android.R.attr.inputType};
TypedArray standardAttrArray = getContext().obtainStyledAttributes(attrs,values);
try {
    mInputType = standardAttrArray.getInt(0,EditorInfo.TYPE_NULL);
} finally {
    standardAttrArray.recycle();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐