Java TextInputLayout setError()不';不要出现在碎片内部

Java TextInputLayout setError()不';不要出现在碎片内部,java,android,Java,Android,我为TextInputLayout的setError()方法编写的文本没有出现在片段中。尝试了各种解决方案,但没有一种有效 <itdgroup.myhomedoc.SignUpTextInputLayout android:id="@+id/email_input_layout1" android:layout_width="wrap_content" android:layout_height="wrap_content" app:errorTextApp

我为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>
这是自定义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));
        }
    }
```

在覆盖的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");
    }
} 

在覆盖的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");
    }
} 


首先分享你的代码,什么是SignupPutLayout?我添加了代码。这是一个自定义文本布局,我创建了如何初始化
emailTIL
?emailTIL=v.findViewById(R.id.email\u input\u layout1);首先分享你的代码,什么是SignupPutLayout?我添加了代码。这是一个自定义文本布局,我创建了如何初始化
emailTIL
?emailTIL=v.findViewById(R.id.email\u input\u layout1);而不是将setError方法设置为TextInputYout,你试过直接将其设置为EditText吗?这是可行的,但不是我设计的背景和下方的文本。问题是,我的代码在活动中运行良好,但在片段中运行不正常。你可以发送一个屏幕截图或你想要的图像吗?我发现了一些可能对你有帮助的东西,试试这个:不要将setError方法设置为TextInpuLayout,您是否尝试过将其直接设置为EditText?这是可行的,但与我设计的背景和下方文本不同。问题是,我的代码在活动中运行良好,但在片段中运行不正常。您是否可以发送一个屏幕截图或您希望的图像?我发现了一些可能对您有所帮助的内容,请尝试以下内容: