[英]TextInputLayout setError() doesn't appear inside fragment
我為 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));
}
}
```
在你重寫的 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");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.