使用TextUtils.isEmpty简单化代码

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

  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 实现了这个接口

posted @ 2014-08-05 02:03  Hellcxs  阅读(405)  评论(0编辑  收藏  举报