Android:在Number/Phone上设置inputType时,检查EditText是否为空 [英] Android: Check if EditText is Empty when inputType is set on Number/Phone

查看:167
本文介绍了Android:在Number/Phone上设置inputType时,检查EditText是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android中有一个EditText供用户输入其AGE.将其设置为inputType = phone. 我想知道是否可以检查此EditText是否为空.

I have an EditText in android for users to input their AGE. It is set an inputType=phone. I would like to know if there is a way to check if this EditText is null.

我已经查看了以下问题:检查EditText是否为空.但这不能解决inputType = phone的情况.

I've already looked at this question: Check if EditText is empty. but it does not address the case where inputType=phone.

这些,我已经检查过了,不能正常工作:

These, I've checked already and do not work:

(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.

谢谢您的帮助.

推荐答案

您可以使用类似TextUtils的类进行检查

You can check using the TextUtils class like

TextUtils.isEmpty(ed_text);

或者您可以像这样检查:

or you can check like this:

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:在Number/Phone上设置inputType时,检查EditText是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆