Android AutoCompleteTextView underline的可绘制选择器中的自定义状态在任何其他状态之前应用,即使为false
我正在使用一个包含AutoCompleteTextView的TextInputLayout,我希望在调用方法Android AutoCompleteTextView underline的可绘制选择器中的自定义状态在任何其他状态之前应用,即使为false,android,android-custom-view,autocompletetextview,android-selector,android-textinputlayout,Android,Android Custom View,Autocompletetextview,Android Selector,Android Textinputlayout,我正在使用一个包含AutoCompleteTextView的TextInputLayout,我希望在调用方法TextInputLayout.setError()时,它的下划线为红色 我在val/attrs中创建了一个名为state\u error的属性,一个用于覆盖setError()方法的CustomTextInputLayout,以及一个用于将此新状态添加到其状态的CustomAutoCompleteTextView 我对选择器的定义如下: <?xml version="1.0" en
TextInputLayout.setError()
时,它的下划线为红色
我在val/attrs中创建了一个名为state\u error
的属性,一个用于覆盖setError()
方法的CustomTextInputLayout,以及一个用于将此新状态添加到其状态的CustomAutoCompleteTextView
我对选择器的定义如下:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/com.example.myapp">
<item android:drawable="@drawable/bg_red_border" myapp:state_error="true" />
<item android:drawable="@drawable/bg_gray_yellow_border" android:state_enabled="true" android:state_focused="true" />
<item android:drawable="@drawable/bg_white_border" />
</selector>
value/attrs.xml
<resources>
<declare-styleable name="custom_text_input_layout">
<attr name="state_error" format="boolean" />
</declare-styleable>
</resources>
CustomAutoCompleteTextView.class
public class CustomTextInputLayout extends TextInputLayout{
public CustomTextInputLayout(Context context) {
super(context);
}
public CustomTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setError(CharSequence error) {
super.setError(error);
if (getEditText() instanceof CustomAutoCompleteTextView) {
((CustomAutoCompleteTextView) getEditText()).setStateError(error != null);
}
}
}
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
private static final int[] STATE_ERROR = {R.attr.state_error};
public boolean mStateError = false;
public CustomAutoCompleteTextView(Context context) {
super(context);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**Overrides the method to add the state_error attribute to the drawable state set only
if its value is true.
*/
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState;
if (mStateError) {
drawableState = super.onCreateDrawableState(extraSpace + 1);
mergeDrawableStates(drawableState, STATE_ERROR);
} else {
drawableState = super.onCreateDrawableState(extraSpace);
}
return drawableState;
}
/**Changes the state_error of the view to toggle the red underline drawable on/off.*/
public void setStateError(boolean stateError) {
if (mStateError != stateError) {
this.mStateError = stateError;
refreshDrawableState();
}
}
}
当我从活动中调用
customTextInputLayout.setError()
时,它工作正常,错误设置和取消设置都很完美,但即使CustomAutoCompleteTextView中的字段stateError
为false时,该行始终是红色的,我替换了xmlns:myapp=”http://schemas.android.com/apk/com.example.myapp"
带有xmlns:myapp=”http://schemas.android.com/apk/res-auto“
而且它似乎起作用了我已经尝试过这两种方法,但似乎都不起作用。因此,似乎你选择了一种艰难的方式来装饰背景可绘制,你可以通过创建一个自定义的可绘制的
类来让它变得容易3倍。怎么会这样?请解释。请参阅,注意示例可绘制实现(ErrorDrawable
)及其对EditText-EditText
i替换的xmlns:myapp=”的示例用法http://schemas.android.com/apk/com.example.myapp“
与xmlns:myapp=”http://schemas.android.com/apk/res-auto"
而且它似乎起作用了,我已经尝试过这两种方法,但似乎都不起作用。因此,似乎您选择了一种艰难的方式来装饰背景可绘制,您可以通过创建一个自定义的可绘制的
类来让它变得容易3倍。怎么会这样?请解释。请参阅注释示例可绘制实现(ErrorDrawable
)及其在EditText
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
private static final int[] STATE_ERROR = {R.attr.state_error};
public boolean mStateError = false;
public CustomAutoCompleteTextView(Context context) {
super(context);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**Overrides the method to add the state_error attribute to the drawable state set only
if its value is true.
*/
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState;
if (mStateError) {
drawableState = super.onCreateDrawableState(extraSpace + 1);
mergeDrawableStates(drawableState, STATE_ERROR);
} else {
drawableState = super.onCreateDrawableState(extraSpace);
}
return drawableState;
}
/**Changes the state_error of the view to toggle the red underline drawable on/off.*/
public void setStateError(boolean stateError) {
if (mStateError != stateError) {
this.mStateError = stateError;
refreshDrawableState();
}
}
}