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

猜你喜欢

转载自nid007.iteye.com/blog/2070546