在BMI计算器中计算按钮

时间:2014-03-12 14:20:25

标签: android

再次是我...我想问一下如果用户不输入他们的体重和重量,我的“按钮”(calculateButton)不会起作用。身高......或者用户不输入自己的体重或身高......希望你能帮助我......谢谢..

以下是我的calculateButton的代码:

   public void calculateClickHandler(View view) {

    if (view.getId() == R.id.calculateButton) {

                }

        EditText weightText = (EditText) findViewById(R.id.weightT);
        EditText heightText = (EditText)findViewById(R.id.heightT);
        TextView resultText = (TextView)findViewById(R.id.resultLabel);
        TextView categoryText = (TextView)findViewById(R.id.categoryLabel);
        rGroup  = (RadioGroup)findViewById(R.id.rGroup);


    int weight = (int)  Float.parseFloat(weightText.getText().toString());
    int height = (int) Float.parseFloat(heightText.getText().toString());
        int bmiValue = calculateBMI(weight, height);

    String bmiInterpretation = interpretBMI(bmiValue);

    resultText.setText("Your BMI is:" + " " + bmiValue + " " + "and you're");
        categoryText.setText(bmiInterpretation + ".");}

1 个答案:

答案 0 :(得分:1)

您需要在代码中插入“验证”。在执行任何操作之前,基本上检查stringEditText的值。

类似的东西:

    rGroup  = (RadioGroup)findViewById(R.id.rGroup);

if( !weightText.getText().toString.equals(" ")){
    int weight = (int)  Float.parseFloat(weightText.getText().toString());
    int height = (int) Float.parseFloat(heightText.getText().toString());
        int bmiValue = calculateBMI(weight, height);
    }  

参见:
Android, Validate an empty EditText field
https://sites.google.com/site/khetiyachintan/sample-example/edit-text-validation-example
Android EditText validation