我正在使用一个包含AutoCompleteTextView的TextInputLayout ,并且我希望在调用方法时它的下划线是红色的。TextInputLayout.setError()
我创建了一个名为state_error
val/attrs 的属性,一个用于覆盖方法的CustomTextInputLayout和一个用于将这个新状态添加到其状态的CustomAutoCompleteTextView 。setError()
我已经像这样定义了选择器:
<?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>
简而言之,当调用 my 时customTextInputLayout.setError()
,它会调用其CustomAutoCompleteTextView,setErrorState(boolean)
这会将状态更改为true
or false
(取决于接收到的值)并调用refreshDrawableState()
更改可绘制对象。
但是,下划线从一开始就显示为红色而不是白色,即使state_error
字段为false
。怎么会这样?其他州似乎没有工作。如果视图获得焦点,则线不会变为黄色,当它失去焦点时,它会变回红色,而不是白色......
如有必要,请参阅下面的代码。
activity_main.xml(只是相关的部分)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.example.myapp.CustomTextInputLayout
android:id="@+id/et_nom"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_weight="1">
<com.example.myapp.CustomAutoCompleteTextView
android:background="@drawable/my_selector"
android:layout_width="match_parent"
android:hint="@string/nom"
android:maxLength="25"
android:duplicateParentState="true"
android:inputType="text"
android:imeOptions="actionNext"
android:nextFocusRight="@+id/et_primer_cognom" />
</com.example.myapp.CustomTextInputLayout>
</LinearLayout>
值/attrs.xml
<resources>
<declare-styleable name="custom_text_input_layout">
<attr name="state_error" format="boolean" />
</declare-styleable>
</resources>
CustomTextInputLayout.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);
}
}
}
CustomAutoCompleteTextView.class
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