如何检查EditText是否在Android / Java中有值[重复]

时间:2022-06-20 17:11:40

This question already has an answer here:

这个问题在这里已有答案:

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?

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

7 个解决方案

#1


38  

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

请比较字符串值而不是==但是等于():

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

#2


9  

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

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

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

#3


2  

Rather what you can check is like:

而你可以检查的是:

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

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

#4


0  

Maybe like this?

也许是这样的?

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

#5


0  

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通知。

#6


0  

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.

它是完全免费的,你可以用它做任何你喜欢的事情。

#7


0  

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

#1


38  

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

请比较字符串值而不是==但是等于():

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

#2


9  

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

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

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

#3


2  

Rather what you can check is like:

而你可以检查的是:

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

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

#4


0  

Maybe like this?

也许是这样的?

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

#5


0  

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通知。

#6


0  

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.

它是完全免费的,你可以用它做任何你喜欢的事情。

#7


0  

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