0

我正在努力如何使用库 Saripaar 为 TextInputLayout 设置错误。我为某些应该验证的输入添加了 TextWatcher。我也注册了适配器

 validator.registerAdapter(TextInputLayout.class,
            new ViewDataAdapter<TextInputLayout, String>() {
                @Override
                public String getData(TextInputLayout flet) throws ConversionException {
                    return flet.getEditText().getText().toString();
                }
            }
    );

但我不确定如何为布局设置错误。

@Override
public void onValidationFailed(List<ValidationError> errors) {
    for (ValidationError error : errors) {
        View view = error.getView();
        String message = error.getCollatedErrorMessage(this);

        if (view instanceof EditText) {
            (EditText)view.setError(message);
        } else {
            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        }
    }
}

感谢您的回答

4

3 回答 3

1

我改变了我的代码,它正在工作

首先,我将绑定视图更改为文本输入布局

@Order(1)
@NotEmpty(messageResId = R.string.error_required_field)
@Email(messageResId = R.string.error_invalid_email)
@BindView(R.id.input_layout_email) TextInputLayout inputLayoutEmail;

然后我覆盖了库中的两个方法

 @Override
public void onValidationSucceeded() {
    loginButton.setAlpha(1);
    loginButton.setEnabled(true);
    MyTextWatcherUtils.clearAllInputs(inputLayoutList);
}

@Override
public void onValidationFailed(List<ValidationError> errors) {
    MyTextWatcherUtils.clearAllInputs(inputLayoutList);
    for (ValidationError error : errors) {
        View view = error.getView();
        String message = error.getCollatedErrorMessage(this);

        if (view instanceof TextInputLayout) {
            ((TextInputLayout) view).setError(message);
        }
    }
}

每次布局都必须清晰。

于 2016-11-24T13:10:48.440 回答
0

我已经更改了我的代码并且它正在工作......!

我正在努力使用 Saripaar 库为 TextInputLayout 设置错误。我为一些需要验证的条目添加了 TextWatcher。我还创建了一个适配器

TextInputLayoutAdapter.class

public class TextInputLayoutAdapter implements ViewDataAdapter<TextInputLayout, String> {
@Override
public String getData(TextInputLayout view) throws ConversionException {
    return view.getEditText().getText().toString();
    }
}

之后定义您的验证

@NotEmpty
@BindView(R.id.textInputName)
TextInputLayout textInputName;

然后实现 Validator.ValidationListener 并定义 Validator 如下:

public class YourActivity extends AppCompatActivity implements Validator.ValidationListener {

private Validator validator;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_your);
    unbinder = ButterKnife.bind(this);
    initValidator();
}


private void initValidator() {
    validator = new Validator(this);
    validator.setValidationListener(this);
    validator.registerAdapter(TextInputLayout.class, new TextInputLayoutAdapter());
    validator.setViewValidatedAction(view -> {
        if (view instanceof TextInputLayout) {
            ((TextInputLayout) view).setError("");
            ((TextInputLayout) view).setErrorEnabled(false);
        }
    });
}

 @Override
 public void onValidationSucceeded() {
 }

 @Override
 public void onValidationFailed(List<ValidationError> errors) {
    for (ValidationError error : errors) {
        View view = error.getView();
        String message = error.getCollatedErrorMessage(this);
        // Display error messages ;)
        if (view instanceof TextInputLayout) {
            ((TextInputLayout) view).setError(message);
        } else {
            showToast(message);
        }
    }
}

试试上面的代码。

快乐编码..!!

于 2018-12-28T10:15:24.830 回答
0

我正在使用android-sarippar 2.0.3版本进行验证。如果您在 xml 中使用这样的嵌套 TextInputEditText 然后

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Phone">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/input_edit_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

您可以像这样跟踪输入布局

public class YourActivity extends AppCompatActivity implements Validator.ValidationListener ,View.OnClickListener {
    private static final String TAG = "EmployerSignUpActivity";

    private TextInputLayout inputLayout;
    @NotEmpty
    @Digits(messageResId = R.id.employer_sign_up_phone_edit_text ,integer = 10)
    private TextInputEditText editText;
    private Validator validator;
    private MaterialButton nextBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);
        nextBtn = findViewById(R.id.next_btn);


        // your code...
        validator = new Validator(this);
        validator.setValidationListener(this);
        nextBtn.setOnClickListener(this);

    }
    @Override
    public void onClick(View view) {
        int id = view.getId();

        switch (id){
            case R.id.next_btn:
                validator.validate();
                break;
        }
    }
        @Override
        public void onValidationSucceeded() {
        Toast.makeText(this, "Yay! we got it right!",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onValidationFailed(List<ValidationError> errors) {
        for (ValidationError error : errors) {
            View view = error.getView();
            String message = error.getCollatedErrorMessage(this);

            // Display error messages ;)
            if (view instanceof TextInputEditText) {
                // this will get TextInputEditText parent which is TextInputLayout
                ((TextInputLayout) findViewById(view.getId()).getParent().getParent()).setError(message);                 } else {
                Toast.makeText(this, message, Toast.LENGTH_LONG).show();
            }
        }
}

}

这就是我在 TextInputLayout 中显示验证错误的方式

于 2020-01-02T20:53:08.743 回答