繁体   English   中英

如何检查其中一个编辑文本是否为空

[英]How to check if one of the edit text is empty

我有2个编辑文本,如何检查其中一个编辑文本是否为空,目前我正在做的是

if(tvPhoneNumber.getText().toString().matches("")){
                if(tvContryCode.getText().length() == 0){
                    //show error
                    Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT).show();
                }else{
                    phoneNumber = tvPhoneNumber.getText().toString();
                    profile.put("phone_number", selectedCountryDialCode.concat(phoneNumber));
                }}else{
                profile.put("phone_number", selectedCountryDialCode.concat(phoneNumber));
            }

您可以使用String.isEmpty()方法检查String是否为空。

注意:此方法不检查String是否为null。

if(tvContryCode.getText().isEmpty() || tvPhoneNumber.getText().isEmpty()){
  // one of the two is empty
}

编辑1:或者如果您想知道哪一个是空的

if(tvContryCode.getText().isEmpty()){
  // tvContryCode is empty
}
else if(tvPhoneNumber.getText().isEmpty()){
  // tvPhoneNumber is empty
}
// Edit 2:
else{
  // none of the textviews is empty
}

编辑3:

if(tvContryCode.getText().isEmpty() && tvPhoneNumber.getText().isEmpty()){
  // Both of the tv´s are empty
}

您可以使用apache lang3库 它将检查为空,null或空白

if(StringUtils.isBlank(tvContryCode.getText()) ){
}

if(StringUtils.isBlank(tvPhoneNumber.getText())){          
}

使用TextUtils.isEmpty()方法

if (TextUtils.isEmpty(tvPhoneNumber.getText().toString())) {
    ...
} else {
    profile.put("phone_number", selectedCountryDialCode.concat(phoneNumber));
}

暂无
暂无

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

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