gpt4 book ai didi

android - 在自定义 View 上设置内部 edittext xml 属性

转载 作者:行者123 更新时间:2023-11-30 01:34:29 25 4
gpt4 key购买 nike

我创建了一个由 EditTextTextView 组成的复合 View ...我想让任何使用我的 View 的开发人员都能够执行以下操作

<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);
...
}

最佳答案

你可以像这样声明样式属性:

<declare-styleable name="MyCustomView">
<attr name="android:inputType"/>
</declare-styleable>

然后像这样在您的自定义 View 中获取此属性:

int inputType = a.getInt(R.styleable.MyCustomView_android_inputType, EditorInfo.TYPE_NULL);

您可以从here获得更详细的答案

关于android - 在自定义 View 上设置内部 edittext xml 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35280966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com