如何确保edittext字段不为空?

时间:2012-09-20 01:59:17

标签: android android-edittext toast

我有2个edittext字段,需要项目名称(字符串)和数量(整数)。如果用户在两个字段上都没有输入任何内容,或者即使只有一个字段为空,我也要显示一个“请输入项目”或“请输入数量”的祝酒词。它创造和错误。以下是以下片段:

 public void onClick(DialogInterface dialog, int id) {

                        String item = itemET.getText().toString();
                        int quantity = Integer.parseInt(quantityET.getText().toString());
                        if(itemET.equals("")){
                            Toast t =Toast.makeText(GroceryList.this, "Please enter item", 5000);
                            t.show();   
                            }
                            if(quantityET.equals("")){
                            Toast t =Toast.makeText(GroceryList.this, "Please enter quantity or enter 0 if none", 5000);
                            t.show();
                            }

这是logcat中的错误:

09-19 10:01:51.766: E/AndroidRuntime(527): FATAL EXCEPTION: main
09-19 10:01:51.766: E/AndroidRuntime(527): java.lang.NumberFormatException: unable to parse '' as integer
09-19 10:01:51.766: E/AndroidRuntime(527):  at java.lang.Integer.parseInt(Integer.java:362)
09-19 10:01:51.766: E/AndroidRuntime(527):  at java.lang.Integer.parseInt(Integer.java:332)
09-19 10:01:51.766: E/AndroidRuntime(527):  at com.mexican.recipes.GroceryList$1$1.onClick(GroceryList.java:60)
09-19 10:01:51.766: E/AndroidRuntime(527):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
09-19 10:01:51.766: E/AndroidRuntime(527):  at android.os.Handler.dispatchMessage(Handler.java:99)
 09-19 10:01:51.766: E/AndroidRuntime(527):     at android.os.Looper.loop(Looper.java:123)
 09-19 10:01:51.766: E/AndroidRuntime(527):     at android.app.ActivityThread.main(ActivityThread.java:3683)
 09-19 10:01:51.766: E/AndroidRuntime(527):     at java.lang.reflect.Method.invokeNative(Native Method)
 09-19 10:01:51.766: E/AndroidRuntime(527):     at java.lang.reflect.Method.invoke(Method.java:507)
 09-19 10:01:51.766: E/AndroidRuntime(527):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 09-19 10:01:51.766: E/AndroidRuntime(527):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 09-19 10:01:51.766: E/AndroidRuntime(527):     at dalvik.system.NativeStart.main(Native Method)

6 个答案:

答案 0 :(得分:1)

这段代码中有两个单独的问题:在一个空字符串上调用parseInt会引发一个异常(你看到的错误),并且你正在对EditText对象本身进行.equals比较,而不是在里面包含的字符串执行以下操作:

public void onClick(DialogInterface dialog, int id) {

   String itemString = itemET.getText().toString();
   String quantityString = quantityET.getText().toString();
   int quantity;

   if(itemString.equals(""))
   {
      Toast t =Toast.makeText(GroceryList.this, "Please enter item", 5000);
      t.show();   
   }

   if(quantityString.equals("")){
      Toast t =Toast.makeText(GroceryList.this, "Please enter quantity or enter 0 if none", 5000);
      t.show();
   }
   else {
      quantity = Integer.parseInt(quantityString);
   }
}

如果您提供的不是数字(例如,如果您在框中键入“a”),这仍然会崩溃,但它应该能够处理任何未输入的内容。

答案 1 :(得分:1)

请注意,您可以使用setError(“error”);

答案 2 :(得分:0)

创建EditText时,声明android:inputType="number"属性init只接受数值。

    public void onClick(DialogInterface dialog, int id) 
    {
                String item;
                int quantity;
                if ( itemET != null )
                {
                     item = itemET.getText().toString();
                }

                if ( quantityET != null && quantityET.getText().toString().trim().length() > 0 )
                {
                     quantity = Integer.parseInt(quantityET.getText().toString().trim());  // trim() method added when converting String to Integer
                }
        if(itemET.getText().trim().equals(""))  // Modification done here
        {
            Toast t =Toast.makeText(GroceryList.this, "Please enter item", 5000);
            t.show();   
        }
        if(quantityET.getText().trim().equals(""))  // Modification done here
        {
            Toast t =Toast.makeText(GroceryList.this, "Please enter quantity or enter 0 if none", 5000);
            t.show();
        }
    }

答案 3 :(得分:0)

以上2个答案都不好,因为:

String item = itemET.getText().toString();

在这里你应该检查item,如果你愿意,但不是这一行是崩溃。

int quantity = Integer.parseInt(quantityET.getText().toString());

这是问题,因为quantityET有一个空字符串而且没有检查过。

有2个解决方案: 一种是检查quantityET.getText()是否为空,长度为> 0 第二个是为Integer.parseInt(value)

设置Try-catch

我会结合2:

String quantity = quantityET.getText().toString();
if(quantity != null && quantity.length() > 0){
   try{
     int iQuantity = Integer.parseInt(quantityET.getText().toString());
   }catch(NumberFormatException nfe){
      Log.e("your tag", e);
   }
}

希望它有所帮助。

答案 4 :(得分:0)

尝试这样的事情:

public void onClick(DialogInterface dialog, int id) {
    int quantity;
    String item;
    if(itemET.getTExt.toString().trim()!="") {
        item = itemET.getText().toString();   
    } else {
        Toast t =Toast.makeText(GroceryList.this, "Please enter item", 5000);
        t.show();   
    }

    if(quantityET.getText().toString().trim()!="") {
        quantity = Integer.parseInt(quantityET.getText().toString().trim());
    } else {
        Toast t =Toast.makeText(GroceryList.this, "Please enter quantity or enter 0 if none", 5000);
        t.show();
}

答案 5 :(得分:0)

        if(editText.getText().toString().length()==0)
        Toast.makeText(Registration.this, "Plz Enter Your name", Toast.LENGTH_LONG).show();

    else if (editText1.getText().toString().length() == 0)
        Toast.makeText(Registration.this, "plz enter mobile no.", Toast.LENGTH_LONG).show();
    else {
        Intent i = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(i);
        finish();
    }