检查编辑文本中的空值

时间:2018-04-02 09:19:26

标签: java android

当我想在我创建的edittext中检查空值时,我收到错误。它对我的源代码来说是错误的

btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = etCode.getText().toString();
                String name = etName.getText().toString();
                float price = Float.parseFloat(etPrice.getText().toString());
                int qty = Integer.parseInt(etQty.getText().toString());
                if(code.length() != 0 && name.length() != 0 && price.length() != 0 && qty.length() != 0){
                 myDB.addData(code, name, price, qty);
                    etCode.setText("");
                    etName.setText("");
                    etPrice.setText("");
                    etQty.setText("");
                }else{
                    Toast.makeText(MainActivity.this,"You must put something in the text field!",Toast.LENGTH_LONG).show();
                }
                }

        });

如何解决我的问题?

7 个答案:

答案 0 :(得分:1)

尝试使用

TextUtils.isEmpty(editText.getText().toString());

通常你不会在文本字段中出现null,但是用上面的行检查它是否为空。所以""和null会给你一个真正的

答案 1 :(得分:1)

我相信您的错误java.lang.NumberFormatException: Invalid float: ""

这是因为您尝试将空值转换为float

你可以这样做。

btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String code = etCode.getText().toString();
            String name = etName.getText().toString();
            String price = etPrice.getText().toString();
            String qty = etQty.getText().toString();
            if(code.isEmpty() && name.isEmpty() && price.isEmpty() && qty.isEmpty(){
                float price1 = Float.parseFloat(etPrice.getText().toString());
                int qty1 = Integer.parseInt(etQty.getText().toString());
                myDB.addData(code, name, price1, qty1);
                etCode.setText("");
                etName.setText("");
                etPrice.setText("");
                etQty.setText("");
            }else{
                Toast.makeText(MainActivity.this,"You must put something in the text field!",Toast.LENGTH_LONG).show();
            }
        }

    });

答案 2 :(得分:0)

使用

if (TextUtils.isEmpty(edittext.getText().toString()) {
    Log.d("ERROR","Edittext is empty");
}

答案 3 :(得分:0)

你可以检查那种方式

if(etCode.getText().toString().trim().equalsIgnoreCase(""))
{
// etCode edittext is empty
}

答案 4 :(得分:0)

试试这个

    /*
     *  for checking String valid or not
     */
    public boolean validateString(String data) {
        return (data != null && !data.isEmpty() && data.length() > 0 && !data.equals("null"));
    }

if(validateString(edittext.getText().toString()){
//Valid String
}

答案 5 :(得分:0)

试试这个:

btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = etCode.getText().toString();
                String name = etName.getText().toString();
                float price = Float.parseFloat(etPrice.getText().toString());
                int qty = Integer.parseInt(etQty.getText().toString());
                if(code.length() != 0 && name.length() != 0 && price != 0 && qty != 0){
                 myDB.addData(code, name, price, qty);
                    etCode.setText("");
                    etName.setText("");
                    etPrice.setText("");
                    etQty.setText("");
                }else{
                    Toast.makeText(MainActivity.this,"You must put something in the text field!",Toast.LENGTH_LONG).show();
                }
                }

        });

答案 6 :(得分:0)

您可以尝试这样:

btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            String code = etCode.getText().toString();
            String name = etName.getText().toString();
            String priceStr = etPrice.getText().toString();
            String qtyStr = etQty.getText().toString();
            float price;
            int qty;
            if(code.length()==0) {
                Toast.makeText(MainActivity.this,"Your code is empty!",Toast.LENGTH_LONG).show();
            }
            if(name.length()==0) {
                Toast.makeText(MainActivity.this,"Your name is empty!",Toast.LENGTH_LONG).show();
            }
            if(priceStr.length()==0) {
                Toast.makeText(MainActivity.this,"Your price is empty!",Toast.LENGTH_LONG).show();
            }else {
                try {
                    price = Float.parseFloat(priceStr);
                }catch (Exception e) {
                    Toast.makeText(MainActivity.this,"Your price is not valid!",Toast.LENGTH_LONG).show();
                }
            }
            if(qtyStr.length()==0) {
                Toast.makeText(MainActivity.this,"Your qty is empty!",Toast.LENGTH_LONG).show();
            }else {
                try {
                    qty = Integer.parseInt(qtyStr);
                }catch (Exception e) {
                    Toast.makeText(MainActivity.this,"Your qty is not valid!",Toast.LENGTH_LONG).show();
                }
            }
                myDB.addData(code, name, price, qty);
                etCode.setText("");
                etName.setText("");
                etPrice.setText("");
                etQty.setText("");
            }

        });