2014-03-12 195 views
-1

这是我再次...我想问如何使我的“按钮”(calculateButton)不起作用,如果用户不会输入他们的体重&高度...或用户不输入自己的体重或他们的身高...希望你可以帮我...感谢..在BMI计算器中计算按钮

这里是我的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 + ".");} 
+0

检查出来的[文件](http://docs.oracle.com/javase/tutorial/java /nutsandbolts/if.html)。 – 323go

回答

1

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

喜欢的东西:

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

+0

我在“.toString!= null”中出现错误...它说的toString无法解析,或者它不是字段... – Deloy

+0

@deloy这是你放置代码的地方,只是给了你一个想法,请研究一下还有一些关于来自其他链接的验证和相关代码 – user2450263