如何检查 EditText 在 Android / Java 中是否有值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3405928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:01:36  来源:igfitidea点击:

How to check if EditText has a value in Android / Java

javaandroid

提问by Allen Gingrich

This should be simple, but I have tried if statements checking for null values and also ones checking the .length of it:

这应该很简单,但是我尝试过 if 语句检查空值以及检查它的 .length 的语句:

EditText marketValLow = (EditText) findViewById(R.id.marketValLow);
EditText marketValHigh = (EditText) findViewById(R.id.marketValHigh);
if (marketValLow.getText().length() != 0 && marketValHigh.getText().length() != 0) {
    Intent intent = new Intent();
    intent.setClass(v.getContext(), CurrentlyOwe.class);
    startActivity(intent);
} else {
    Toast.makeText(CurrentMarketValue.this, "You need to enter a high AND low.", Toast.LENGTH_SHORT);
}

But it doesn't detect nothing was entered. Any ideas?

但它没有检测到没有输入任何内容。有任何想法吗?

采纳答案by Sephy

Please compare string value not with ==but equals():

请将字符串值与==但不比较equals()

String yourString = ;
if (marketValHigh.getText().toString().equals(""))
{
    // This should work!
}

回答by Benny Skogberg

Maybe like this?

也许像这样?

String value = editText.getText().toString();
if (value == "")
{
    // your code here
}

回答by Hare-Krishna

This will check if the edit text is empty or not:

这将检查编辑文本是否为空:

if (marketValLow.getText().toString().trim().equals(""))
{
}

回答by Amit Lad

Rather what you can check is like:

相反,您可以检查的是:

String text = mSearchText.getText().toString();

if (!TextUtils.isEmpty( mSearchText.getText().trim())) {
    // your code
}

回答by Zhehao Mao

If it stops progression to the next activity but doesn't show the toast there must be something wrong in your else statement. Have you read the Dev Guide article on Toast notifications? It is under User Interface -> Notifying the User -> Creating Toast Notifications.

如果它停止前进到下一个活动但没有显示吐司,那么您的 else 语句中一定有问题。您是否阅读过有关 Toast 通知的开发指南文章?它位于用户界面 -> 通知用户 -> 创建 Toast 通知下。

回答by Jaavaaan

Validation is somewhat tedious. Maybe I can help you in a more generic manner. I have written a basic framework to validate fields in Android.

验证有点乏味。也许我可以用更通用的方式帮助你。我编写了一个基本框架来验证 Android 中的字段

It is totally free, and you can do whatever you like with it.

它是完全免费的,您可以随心所欲地使用它。

回答by Sergio Abreu

write a simple function inside a class:

在类中编写一个简单的函数:

    public static boolean isEditTextEmpty( EditText et){
      String s = et.getText().toString();
      return s.equals("");  // really empty.          
   }

    public static boolean isEditTextInvisible( EditText et){
      String s = et.getText().toString();
      return s.trim().equals("");  // might have only spaces...           
   }

OR... with positive Logic:

或...与正逻辑:

    public static boolean hasContentEditText( EditText et){
      String s = et.getText().toString();
      return s != null; // spaces will count.             
   }