Dev/Android
[Android] 안드로이드 TextUtils - TextView 빈값 null 검사
healthyryu
2017. 2. 2. 12:25
[Android] 안드로이드 TextUtils - TextView 빈값 null 검사
안드로이드 개발자 대부류 :)
안드로이드에서 보통 TextView 의 텍스트(string)의 빈값 혹은 null 을 검사하는 방법이 아래와 같을 것입니다.
1 | if (textview.getText() != null && textview.getText().equals("")) { ... } |
저도 처음에 위와 같은 방식으로 검사를 했었습니다.
하지만 안드로이드 자체에서 제공하는 TextUtils 가 존재한고 있었습니다.
TextUtils 를 사용하면 해당 TextView 의 null 체크 및 빈값 여부를 체크합니다. 그렇기에 TextUtils 를 사용해서 TextView를 검사하면 코드도 줄어들고 더 괜찮을것 같습니다.
1 | if (TextUtils.isEmpty(text.getText())) { ... } |
해당 TextUtils.isEmpty() 를 클릭해서 메서드를 확인해보면 아래와 같습니다.
1 2 3 4 5 6 7 8 9 10 11 | /** * Returns true if the string is null or 0-length. * @param str the string to be examined * @return true if str is null or zero length */ public static boolean isEmpty(@Nullable CharSequence str) { if (str == null || str.length() == 0) return true; else return false; } |
공감과 광고클릭으로 응원 부탁해욥 :)
▼▼▼
반응형