Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 有没有办法重用attr inputType?_Android_Attr - Fatal编程技术网

Android 有没有办法重用attr inputType?

Android 有没有办法重用attr inputType?,android,attr,Android,Attr,我有一个自定义的视图组,它包含一个EditText。 我想为xml中的EditText设置inputType。但我不想重新定义输入类型。我该怎么做 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyEditText); a.getInt(android.R.attr.inputType, EditorInfo.TYPE_NULL); 原因:java.lang.ArrayIndexOutOfBoundsExcep

我有一个自定义的视图组,它包含一个EditText。 我想为xml中的EditText设置inputType。但我不想重新定义输入类型。我该怎么做

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyEditText);
a.getInt(android.R.attr.inputType, EditorInfo.TYPE_NULL);
原因:java.lang.ArrayIndexOutOfBoundsException:

错误,因为我的视图组中没有inputType attr。
我也试过:

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


有什么想法吗?谢谢

我找到了这样做的方法

将其添加到attrs.xml中:

<declare-styleable name="MyEditText" >
    <attr name="android:inputType"/>
</declare-styleable>
然后我可以在cusom视图类中获取此属性:

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

您能否发布相关的代码位(XML布局等)?@curtisLoew感谢您的回复。我想办法做到这一点。看看我的答案。谢谢。
if(inputType!=EditorInfo.TYPE_NULL){mEditTextView.setInputType(inputType)}
可以作为charm使用。注意:使用自定义视图名称+\u android\u inputType而不是ClearableEditText\u android\u inputType。
int inputType = a.getInt(R.styleable.MyEditText_android_inputType, EditorInfo.TYPE_NULL);
if (inputType != EditorInfo.TYPE_NULL) {
    mInputEditText.setInputType(inputType);
}