Android:检查空的EditText字段

时间:2012-03-31 22:18:45

标签: android android-edittext try-catch

我正在为个人兴趣开发一个Android项目。我正在使用多个EditText字段,所有字段必须填入非零小数或整数。我在我的xml文件中指定它只接受数字或小数。我不太熟悉Java,但我一直在阅读try / catch方法以及当我尝试将每个EditText值解析为double时如何使用它。现在,它在所有字段都填满时运行正常,但我想添加某种异常处理,这样如果你错误地忘记输入字段,应用程序将显示错误消息而不是崩溃。这听起来很多,但我认为它很小。这是我正在使用的计算方法。感谢您的评论:)

public static void calculate() {

    NumberFormat formatter1 = new DecimalFormat("#0.00");
    NumberFormat formatter2 = new DecimalFormat("#0.00000");

    Editable evG1F = etG1F.getText();
    Editable evG1U = etG1U.getText();
    Editable evG2F = etG2F.getText();
    Editable evG2U = etG2U.getText();
    Editable evG3F = etG3F.getText();
    Editable evG3U = etG3U.getText();
    Editable evWin = etWin.getText();

    double g1f = 0.0;
    double g1u = 0.0;
    double g2f = 0.0;
    double g2u = 0.0;
    double g3f = 0.0;
    double g3u = 0.0;
    double p1odds, p2odds, p3odds, p4odds, p5odds, p6odds, p7odds, p8odds;
    double totalParlay, profit, finalOdds, risk;

    win = 0.0;

    g1f = Double.parseDouble(evG1F.toString());
    g1u = Double.parseDouble(evG1U.toString());
    g2f = Double.parseDouble(evG2F.toString());
    g2u = Double.parseDouble(evG2U.toString());
    g3f = Double.parseDouble(evG3F.toString());
    g3u = Double.parseDouble(evG3U.toString());
    win = Double.parseDouble(evWin.toString());

    p1odds = (1 / (g1f * g2f * g3f));
    p2odds = (1 / (g1f * g2f * g3u));
    p3odds = (1 / (g1f * g2u * g3f));
    p4odds = (1 / (g1f * g2u * g3u));
    p5odds = (1 / (g1u * g2f * g3f));
    p6odds = (1 / (g1u * g2f * g3u));
    p7odds = (1 / (g1u * g2u * g3f));
    p8odds = (1 / (g1u * g2u * g3u));

    totalParlay = (p1odds + p2odds + p3odds + p4odds + p5odds + p6odds
            + p7odds + p8odds);
    profit = 1 - totalParlay;
    finalOdds = ((profit / totalParlay) + 1);
    risk = (win / (finalOdds - 1));

    p1 = ((risk + win) * p1odds);
    p2 = ((risk + win) * p2odds);
    p3 = ((risk + win) * p3odds);
    p4 = ((risk + win) * p4odds);
    p5 = ((risk + win) * p5odds);
    p6 = ((risk + win) * p6odds);
    p7 = ((risk + win) * p7odds);
    p8 = ((risk + win) * p8odds);

    sRisk = risk;
    sFinalOdds = finalOdds;

    tvRisk.setText("$" + formatter1.format(Parlay.sRisk));
    tvOdds.setText("" + formatter2.format(Parlay.sFinalOdds));

}

1 个答案:

答案 0 :(得分:1)

您可以将parseDouble()语句包装在try / catch块中:

    try {
        g1f = Double.parseDouble(evG1F.getText().toString());
        g1u = Double.parseDouble(evG1U.getText().toString());
        g2f = Double.parseDouble(evG2F.getText().toString());
        g2u = Double.parseDouble(evG2U.getText().toString());
        g3f = Double.parseDouble(evG3F.getText().toString());
        g3u = Double.parseDouble(evG3U.getText().toString());
        win = Double.parseDouble(evWin.getText().toString());
    } catch (NumberFormatException e) {
        // Error handling here
    }

如果异常不是有效的double,或者字符串为空,则抛出异常。我在每次调用之前都在getText()之前添加了toString(),因为您实际上不需要进一步声明这些Editable个对象(因为您只使用了getText()无论如何他们。)