我正在创建一个自定义组合视图,其布局如下
<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
中:
我已经想出如何将这些字符串属性分别转发到TextView
和EditText
中:
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LabeledEditText,
0, 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
已声明的值?
label
属性,它设置了TextView
的android:text
和一个hint
属性,它设置了EditText
的android:hint
。这些很简单,因为它们只是字符串。然而,我还想要一个inputType
属性,它设置了EditText
的android:inputType
。但我不想为所有可能的值重新编写代码。 - Code-Apprentice