Is there a way to reuse attr inputType?

I have a custom ViewGroup and contains an EditText. I want to set inputType for EditText in xml. But I don't want to override the input type. How can i do this?

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyEditText);
a.getInt(android.R.attr.inputType, EditorInfo.TYPE_NULL);

      

Caused by: java.lang.ArrayIndexOutOfBoundsException:

The error is because there is no input type attr in my ViewGroup.
I also tried this:

int[] attrsReuse = new int[] { android.R.attr.inputType /* index 0 */};
Resources.Theme theme = context.getTheme();
TypedArray ta = theme.obtainStyledAttributes(attrsReuse);

      

The same mistake.

Any ideas? Thank!

+3


source to share


1 answer


Finally, I find a way to do this:

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

      

And add this to your layout:



android:inputType="textPassword"

      

Then I can get this attr in my class.

int inputType = a.getInt(R.styleable.ClearableEditText_android_inputType, EditorInfo.TYPE_NULL);
if (inputType != EditorInfo.TYPE_NULL) {
    mInputEditText.setInputType(inputType);
}

      

+15


source







All Articles