【发布时间】:2016-05-18 18:48:54
【问题描述】:
我创建了一个由EditText 和TextView 组成的复合视图...我想让任何使用我的视图的开发人员能够执行以下操作
<MyCustomView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:setInputType="textPassword"/>
密切注意该 xml 的最后一行。
如何将该属性连接到位于 MyCustomView 内的 EditText
PD:我已经在 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);
...
}
【问题讨论】:
-
这不是重复的,我已经更新了我的问题。