简体   繁体   English

Android:EditText中的验证符号

[英]Android: Validation symbol in EditText

We have the setError method for setting an error message with a red exclamation symbol at the end of the box if validation of an EditText returns false: 如果EditText的验证返回false,我们有setError方法用于在框的末尾设置带有红色感叹号的错误消息:

验证返回false

I want to set a green tick symbol , just like the red exclamation at the end of the box when my validation returns true: 我想设置一个绿色刻度符号 ,就像我的验证返回true时盒子末尾的红色感叹号一样:

password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {

                if (password.getText().toString().length() < 6) {
                    password.setError("Password should be greater than 6 characters!");
                }
                else {
                    //validation is true, so what to put here?
                }
            }
        }
    });
}

EDIT 1 Seemingly impossible, but there's more to the problem. 编辑1看似不可能,但问题还有更多。 I did this: 我这样做了:

email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                String mail = email.getText().toString();
                if(!android.util.Patterns.EMAIL_ADDRESS.matcher(mail).matches()) {
                    email.setError("Please enter a valid email address");
                } 
                else {
                    Log.i("yay!","Email is valid!!!");
                    email.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.validated, 0);
                }
            }
        }
    });

Though I can see in my logs that yay: Email is valid!!! 虽然我可以在我的日志中看到yay: Email is valid!!! , I can't see the validation symbol at the end of the EditText . ,我无法在EditText的末尾看到验证符号。

However, to my surprise when I change the if statement to always false , I can see both the symbol with the log statement. 但是,令我惊讶的是,当我将if语句更改为始终为false ,我可以看到带有log语句的符号。

Any explanations upon why could this be happening? 有关为什么会发生这种情况的任何解释?

You can show drawableRight when your validation is true. 验证为true时,您可以显示drawableRight

password.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.rightImage, 0);

and set it back to normal when validation is false. 验证为false时将其设置恢复正​​常。

password.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

I just tested it out in my own project using setError(CharSequence error, Drawable icon) 我刚刚使用setError(CharSequence error, Drawable icon)在我自己的项目中测试了它

  1. Get a new Icon: 获取一个新图标:
    Go to my drawable folder 转到我的drawable文件夹
    adding a new vector asset. 添加新的矢量资产。

    I picked "material icon" and browse, 我选择了“素材图标”并浏览,
    then picked ic_done_24pp. 然后选择了ic_done_24pp。

  2. Color: 颜色:
    Next, I went into the xml and made it green by changing the fillcolor: 接下来,我进入xml并通过更改fillcolor使其变为绿色:
    android:fillColor="#FF00FF00"

3: Change code: 3:更改代码:

Drawable myIcon = getResources().getDrawable(R.drawable.ic_done_24dp); 
myIcon.setBounds(0, 0, myIcon.getIntrinsicWidth(), myIcon.getIntrinsicHeight());
mPasswordView.setError("Good", myIcon); 

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM