【问题标题】:Set inner edittext xml properties on custom view在自定义视图上设置内部 edittext xml 属性
【发布时间】:2016-05-18 18:48:54
【问题描述】:

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

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

【问题讨论】:

标签: android xml


【解决方案1】:

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

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

并在您的自定义视图中获取此属性,如下所示:

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

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

【讨论】:

    猜你喜欢
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    相关资源
    最近更新 更多