Android - 拒绝空输入问题

发布于 2024-10-14 18:51:09 字数 1434 浏览 5 评论 0原文

我目前正在尝试让我的代码拒绝空(空)输入,每次我输入任何值时它都会崩溃,但是告诉它拒绝“0”有效(如代码的 else if 部分所示)。

boolean check_input1 = Add.this.input.getText().toString().equals(null);
        boolean check_input2 = Add.this.input2.getText().toString().equals(null);
        boolean check_input3 = Add.this.input3.getText().toString().equals(null);

        float pricecheck_input2 = Float.valueOf(Add.this.input2.getText().toString().trim()).floatValue();
        float pricecheck_input3 = Float.valueOf(Add.this.input3.getText().toString().trim()).floatValue();

        if (check_input1 == true | check_input2 == true | check_input3 == true) {
       // prepare the alert box
               AlertDialog.Builder alertbox = new AlertDialog.Builder(Add.this);

               // set the message to display
               alertbox.setMessage("No fields may be blank");

               alertbox.show();
        }
        else if (pricecheck_input2 == 0 | pricecheck_input3 == 0) {
         // prepare the alert box
               AlertDialog.Builder alertbox = new AlertDialog.Builder(Add.this);

               // set the message to display
               alertbox.setMessage("Prices must be greater than 0");

               alertbox.show();
        }
        else {
         new InsertDataTask().execute(Add.this.input.getText().toString(), Float.toString(abv_ppl_calculation), Add.this.input3.getText().toString());
        }

帮助将不胜感激!

I am currently trying to get my code to reject a null (empty) input, every time I enter nothing for values it crashes, however telling it to reject "0" worked (as seen in the else if part of the code).

boolean check_input1 = Add.this.input.getText().toString().equals(null);
        boolean check_input2 = Add.this.input2.getText().toString().equals(null);
        boolean check_input3 = Add.this.input3.getText().toString().equals(null);

        float pricecheck_input2 = Float.valueOf(Add.this.input2.getText().toString().trim()).floatValue();
        float pricecheck_input3 = Float.valueOf(Add.this.input3.getText().toString().trim()).floatValue();

        if (check_input1 == true | check_input2 == true | check_input3 == true) {
       // prepare the alert box
               AlertDialog.Builder alertbox = new AlertDialog.Builder(Add.this);

               // set the message to display
               alertbox.setMessage("No fields may be blank");

               alertbox.show();
        }
        else if (pricecheck_input2 == 0 | pricecheck_input3 == 0) {
         // prepare the alert box
               AlertDialog.Builder alertbox = new AlertDialog.Builder(Add.this);

               // set the message to display
               alertbox.setMessage("Prices must be greater than 0");

               alertbox.show();
        }
        else {
         new InsertDataTask().execute(Add.this.input.getText().toString(), Float.toString(abv_ppl_calculation), Add.this.input3.getText().toString());
        }

Help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坐在坟头思考人生 2024-10-21 18:51:09

您需要检查空检查的逻辑。您有:

boolean check_input1 = Add.this.input.getText().toString().equals(null);

问题是 toString() 如何导致 null?您的代码处于正确的轨道上,因为稍加挖掘就会发现 .getText() 永远不会返回 null (它始终是 CharSequence)。检查的右侧应该检查空字符串。例如:

boolean check_input1 = Add.this.input.getText().toString().equals("");

You need to examine the logic for your null check. You have:

boolean check_input1 = Add.this.input.getText().toString().equals(null);

The question is how would a toString() to result in null? Your code is on the right track since a little digging shows that .getText() will never return null (it's always a CharSequence). The right side of your check should instead check for the empty string. E.g.:

boolean check_input1 = Add.this.input.getText().toString().equals("");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文