简体   繁体   English

在Android中的两个`EditTexts'上同时显示错误

[英]Simultaneously display error on two `EditTexts` in android

I'm working on a from in android. 我正在使用android中的。 It has a two EditText s, one for name and the other for email. 它有两个EditText ,一个用于名称,另一个用于电子邮件。 I want that when both the fields are blank, the warning should appear on both fields. 我希望当两个字段都为空白时,警告应该同时出现在两个字段上。 Not only the "!" 不仅是“!” mark encircled in red but also the message I type in: 标记用红色圈起来,但我键入的消息也是如此:

Name.setError("Customer's Name is Required!"); Email.setError("Customer's Email is Required!");

Currently it displays the message only on the field which has focus. 当前,它仅在具有焦点的字段上显示消息。 How to show the message on fields which are not focused? 如何在未聚焦的字段上显示消息?

try this way 尝试这种方式

if (Name.getText().toString().equals("")){
                    Name.setError("Customer's Name is Required!");
                }
 if (Email.getText().toString().equals("")){
                    Email.setError("Customer's Email is Required!");
                }

You need to use the TextWatcher on both your EditTexts. 您需要在两个EditText上都使用TextWatcher

Name.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                 // This is your current text, see if it is empty, do your logic
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

Repeat for the Email EditText . 对电子邮件EditText重复上述步骤。

try this its working for me 试试这个对我有用

if (name.equalsIgnoreCase("") || email.equalsIgnoreCase("")) {
               name.setError("Customer's Name is Required!");
                email.setError("Customer's Email is Required!");
            } else {
                // code here
         }

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

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