我创建了一个复合视图,由一个 EditText
和一个 TextView
组成... 我希望任何使用我的视图的开发人员能够执行以下操作:
<MyCustomView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:setInputType="textPassword"/>
请注意xml中的最后一行。我该如何将该属性连接到位于MyCustomView
内部的EditText
上?
附注:我已经在Attrs.xml中创建了一个属性并尝试了这个方法。
<declare-styleable name="ValidateEditText">
<flag name="none" value="0x00000000" />
<flag name="text" value="0x00000001" />
<flag name="textCapCharacters" value="0x00001000" />
<flag name="textCapWords" value="0x00002000" />
<flag name="textCapSentences" value="0x00004000" />
<flag name="textAutoCorrect" value="0x00008000" /> ...
这不起作用...EditText.setContentType由于某些原因无法工作
我已经设置了其他自定义属性,我的问题是如何设置这个特定的属性,"InputType"似乎不起作用。
protected void init(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray typedArray;
typedArray = context.obtainStyledAttributes(attrs, R.styleable.ValidateEditText);
try {
mHint = typedArray.getString(R.styleable.ValidateEditText_hint);
mInputType = typedArray.getInt(R.styleable.ValidateEditText_inputType, EditorInfo.TYPE_CLASS_TEXT);
mMaxLength = typedArray.getInt(R.styleable.ValidateEditText_maxLength, -1);
mSingleLine = typedArray.getBoolean(R.styleable.ValidateEditText_singleLine, false);
mPassword = typedArray.getBoolean(R.styleable.ValidateEditText_password, false);
mImeOption = typedArray.getInt(R.styleable.ValidateEditText_imeOptions, EditorInfo.IME_ACTION_NEXT);
mEditable = typedArray.getBoolean(R.styleable.ValidateEditText_editable, true);
} catch (Exception e) {
e.printStackTrace();
} finally {
typedArray.recycle();
}
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
//Initialise views
mErrorMessage = (TextView) findViewById(R.id.error_msg);
mEditText = (EditText) findViewById(R.id.edit_text);
mEditText.setInputType(mInputType);
...
}