【问题标题】:TextInputLayout setError() doesn't appear inside fragmentTextInputLayout setError() 没有出现在片段内
【发布时间】:2019-10-02 06:56:23
【问题描述】:

我为 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));
        }
    }
```

【问题讨论】:

  • 先分享你的代码
  • 什么是SignUpTextInputLayout?
  • 我添加了代码。这是我创建的自定义 textIputLayout
  • 你是如何初始化emailTIL的?
  • emailTIL = v.findViewById(R.id.email_input_layout1);

标签: java android


【解决方案1】:

在你重写的 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方法设置为TextInpuLayout,你有没有试过直接设置为EditText?
  • 这是有效的,但不是我设计的背景和文字
  • 问题是我的代码在活动中运行良好,但在片段中却没有
  • 你能发一张你想要的截图或图片吗?
  • 我找到了一些可能对你有帮助的东西,试试这个:stackoverflow.com/questions/36229634/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多