使用TextUtils.isEmpty简单化代码

如果让你判断一个文本框是否为空(null)或者没有任何值(length=0),你会怎么怎样去写代码,很多初学者可能会这样写:

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

{........}

其实没有必要这么麻烦,在android的官方API中有更方便的解决方法:Returns true if the string is null or 0-length,可以使用这个方法-TextUtils.isEmpty()

可以这样写:

if(!TextUtils.isEmpty(text))

{.....}

posted @ 2016-10-08 17:50  Coder_Peter  阅读(2393)  评论(0编辑  收藏  举报