我正在努力如何使用库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();
}
}
}
感谢您的回答
答案 0 :(得分: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);
}
}
}
每次都必须清楚布局。
答案 1 :(得分: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);
}
}
}
尝试上面的代码。
快乐编码.. !!
答案 2 :(得分: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中显示验证错误的方式