2017-10-19 132 views
-1

我有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)); 
      } 
+0

使用條件1 || condition2? – 2017-10-19 05:54:58

+0

你能寫答案嗎? – blackHawk

+0

只需列出必填字段,然後像EditText [] mandatory = {edit1,edit2}; (編輯:必填){非空(編輯);}' – AxelH

回答

1
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 
} 
+0

如果tvCountry代碼不爲空,它會評估其他語句嗎? – blackHawk

+0

編輯你的問題,如果兩者都不爲空然後 – blackHawk

+0

當我們應用&&和||時,你說什麼?運營商 – blackHawk

1

您可以檢查是否一個String是空的或不使用方法String.isEmpty()

注意:此方法不檢查字符串是否爲空。

+0

如果兩個編輯文本之一爲空,那麼如果條件爲空 – blackHawk

1

您可以使用apache lang3 library。它將檢查空,空或空白

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

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

使用TextUtils.isEmpty()方法

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