---
title: "使用TextUtils.isEmpty简化代码"
author: "nid007"
date: "2014-05-23T21:06:41Z"
tags: []
categories: ["移动开发"]
slug: "textutils-isempty"
draft: false
lastmod: "2014-05-23T21:06:41Z"
id: 2070546
url: "https://nid007.iteye.com/blog/2070546"
---

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

```java

  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里


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

```


所以我们可以使用

```java
TextUtils.isEmpty(text)
```

代替

```java
if(text == null || text.length() == 0)
```


CharSequence 是一个接口，String 实现了这个接口