Android:在Number / Phone上设置inputType时,检查EditText是否为空

我在 Android中有一个EditText,用户可以输入他们的AGE.它设置为inputType = phone.
我想知道是否有办法检查此EditText是否为空.

我已经看过这个问题:Check if EditText is empty.,但它并没有解决inputType = phone的情况.

这些,我已经检查了,不工作:

(EditText) findViewByID(R.id.age)).getText().toString() == null
(EditText) findViewByID(R.id.age)).getText().toString() == ""
(EditText) findViewByID(R.id.age)).getText().toString().matches("")
(EditText) findViewByID(R.id.age)).getText().toString().equals("")
(EditText) findViewByID(R.id.age)).getText().toString().equals(null)
(EditText) findViewByID(R.id.age)).getText().toString().trim().length() == 0
(EditText) findViewByID(R.id.age)).getText().toString().trim().equals("")
and isEmpty do not check for blank space.

感谢您的帮助.

解决方法

您可以使用以下任一方式检查:
EditText ed = (EditText) findViewById(R.id.age);

    String ed_text = ed.getText().toString().trim();

    if(ed_text.isEmpty() || ed_text.length() == 0 || ed_text.equals("") || ed_text == null)
    {
        //EditText is empty
    }
    else
    {
        //EditText is not empty
    }

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...