[英]Simultaneously display error on two `EditTexts` in android
我正在使用android中的。 它有兩個EditText
,一個用於名稱,另一個用於電子郵件。 我希望當兩個字段都為空白時,警告應該同時出現在兩個字段上。 不僅是“!” 標記用紅色圈起來,但我鍵入的消息也是如此:
Name.setError("Customer's Name is Required!"); Email.setError("Customer's Email is Required!");
當前,它僅在具有焦點的字段上顯示消息。 如何在未聚焦的字段上顯示消息?
嘗試這種方式
if (Name.getText().toString().equals("")){
Name.setError("Customer's Name is Required!");
}
if (Email.getText().toString().equals("")){
Email.setError("Customer's Email is Required!");
}
您需要在兩個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) {
}
});
對電子郵件EditText
重復上述步驟。
試試這個對我有用
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.