ITeye博客

使用TextUtils.isEmpty简化代码

nid007 · 2014-05-23 21:06 · android · 移动开发 · Markdown

我们经常看到这样的代码:


  public void setText(String text , TextView view , int string){
    	if(text == null || text.length() == 0){
    		// do something
    	}
  }

其实在android里 if(text ==null || text.length()==0)是有封装的。 在android.text.TextUtils里


public static boolean isEmpty(CharSequence str) {
        if (str == null || str.length() == 0)
            return true;
        else
            return false;
    }

所以我们可以使用

TextUtils.isEmpty(text)

代替

if(text == null || text.length() == 0)

CharSequence 是一个接口,String 实现了这个接口

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。