You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中Java代码的If Else输入校验失效问题求助

问题排查:EditText与RadioGroup输入校验逻辑失效

我在Android Studio的Java代码中编写了Calc方法,目的是校验EditText输入框是否为空、RadioGroup是否未选中,若存在空输入则通过Toast弹出“No Inputs”提示,但这段If Else校验逻辑失效。移除该校验代码后,其他业务逻辑可正常运行,请求协助排查解决。

原代码:

public void Calc(View v) {
    radioButton = findViewById(R.id.byjY);

    TextView result = (TextView) findViewById(R.id.textViewResultDisplay);
    EditText et1 = (EditText) findViewById(R.id.EditTextBYJnextpageq2);
    float n1 = Integer.parseInt(et1.getText().toString());

    // This is the line that have error
    if (et1.getText().toString().length() == 0 || radioGroup.getCheckedRadioButtonId() == -1){
        Toast.makeText(this, "No Inputs", Toast.LENGTH_SHORT).show();
    } // The line above is not working. If I removed the above codes however, it works fine

    else if (radioButton.isChecked() && !et1.equals("")) {
        float totalML = 2000 * n1; //one month = 2000ml = 4 bottles
        float totalBottles = totalML / 500;
        float totalDays = totalML / 60; //total volume / 60ml (per day)
        //Toast.makeText(BYJpage2.this, "Go Go Power Rangers", Toast.LENGTH_SHORT).show();
        result.setText("Batman");
    } else if (!radioButton.isChecked() && !et1.equals("")) {
        float totalML = 1000 * n1; //one month = 1000ml = 2 bottles
        float totalBottles = totalML / 500;
        float totalDays = totalML / 30; //total volume / 30ml (per day)

        result.setText("Superman");
    }
}

问题原因及修复步骤

  • 先校验再解析数值:原代码在执行校验前就调用Integer.parseInt(),如果EditText为空会直接抛出NumberFormatException,导致校验逻辑根本没机会执行。必须把校验代码移到数值解析之前。
  • 初始化RadioGroup:代码中使用了radioGroup但未初始化,需要添加radioGroup = findViewById(R.id.你的RadioGroupID);(替换成你布局中RadioGroup的实际ID),否则会出现空指针或者无法正确判断选中状态。
  • 修正空判断逻辑:!et1.equals("")是错误的,EditText对象不能直接和字符串比较,应该用!et1.getText().toString().isEmpty(),或者用TextUtils.isEmpty()来兼容空白字符的情况。

修正后的完整代码

public void Calc(View v) {
    // 初始化RadioGroup,替换成你布局中RadioGroup的实际ID
    RadioGroup radioGroup = findViewById(R.id.你的RadioGroupID);
    RadioButton radioButton = findViewById(R.id.byjY);

    TextView result = findViewById(R.id.textViewResultDisplay);
    EditText et1 = findViewById(R.id.EditTextBYJnextpageq2);
    String inputText = et1.getText().toString().trim();

    // 先执行校验逻辑
    if (TextUtils.isEmpty(inputText) || radioGroup.getCheckedRadioButtonId() == -1) {
        Toast.makeText(this, "No Inputs", Toast.LENGTH_SHORT).show();
        return; // 校验不通过直接返回,不执行后续逻辑
    }

    // 校验通过后再解析数值
    float n1 = Integer.parseInt(inputText);

    if (radioButton.isChecked()) {
        float totalML = 2000 * n1;
        float totalBottles = totalML / 500;
        float totalDays = totalML / 60;
        result.setText("Batman");
    } else {
        float totalML = 1000 * n1;
        float totalBottles = totalML / 500;
        float totalDays = totalML / 30;
        result.setText("Superman");
    }
}

内容的提问来源于stack exchange,提问作者Jane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 11:45:29