TextInputLayout setError()未出现在片段内

时间:2019-10-02 06:56:23

标签: java android

我为TextInputLayout的setError()方法编写的文本未出现在片段内。尝试了各种解决方案,但没有一个起作用……

<itdgroup.myhomedoc.SignUpTextInputLayout
    android:id="@+id/email_input_layout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:errorTextAppearance="@style/error_appearance"
    app:errorEnabled="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textViewAvenirBookFont">

<itdgroup.myhomedoc.CustomEditText
    android:id="@+id/input_email"
    android:layout_width="295dp"
    android:layout_height="50dp"
    android:layout_marginTop="11dp"
    android:maxLines="1"
    android:background="@drawable/custom_signup_edit_text"
    android:imeOptions="actionNext"
    android:inputType="textEmailAddress"
    android:nextFocusDown="@+id/input_password"
    android:paddingStart="20dp"
    android:paddingEnd="20dp"
    android:textSize="14sp"/>

</itdgroup.myhomedoc.SignUpTextInputLayout>
                String strUserName = eMail.getText().toString();
                if(TextUtils.isEmpty(strUserName) || !isEmailValid(strUserName)) {
                    emailTIL.setError("Please enter valid email");
                    return;

这是自定义的TextInputLayout

public class SignUpTextInputLayout extends TextInputLayout {
    private Context context;

    public SignUpTextInputLayout(Context context) {
        super(context);
        this.context = context;
    }

    public SignUpTextInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public SignUpTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();

        EditText editText = getEditText();
        if(editText != null) {
            editText.setBackground(ContextCompat.getDrawable(this.context, R.drawable.custom_signup_edit_text));
        }
    }

    @Override
    public void setError(@Nullable final CharSequence error) {
        super.setError(error);

        EditText editText = getEditText();
        if(editText != null) {
            editText.setBackground(ContextCompat.getDrawable(this.context, R.drawable.error_signup_edit_text));
        }
    }
```

1 个答案:

答案 0 :(得分:0)

在您覆盖的setError方法中,尝试同时添加setError方法:

@Override
public void setError(@Nullable final CharSequence error) {
    super.setError(error);

    EditText editText = getEditText();
    if(editText != null) {
        editText.setBackground(ContextCompat.getDrawable(this.context, R.drawable.error_signup_edit_text));
        editText.setError("Please enter valid email");
    }
}