【发布时间】:2012-04-01 17:26:43
【问题描述】:
我有一个 CompositeComponent (EditText+ImageButton) 单击按钮时,edittext 内容将被清除。 它工作正常。我的问题是为我的组件设置属性。我正在使用 declare-styleable 为我的组件设置属性。
我成功设置了 minLines、maxLines 和 textColor。
如何通过 xml 将 inputtype 设置为我的组件。
我的属性.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>
以及main_layout.xml中mycomponent的用法:
<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--------> >
我不能在我的 attribute.xml 中使用 ENUM。
如何在我的cet:InputType 中引用android:inputType="numberSigned"?
编辑:
这就是我在 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 没有问题。
【问题讨论】:
-
我没有收到任何错误。它工作正常。我不知道如何分配输入类型??而不是给出原始整数值,我想在android中使用枚举(android:inputtype)。
标签: android custom-component attr