以下是我的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 :(得分:1)
您需要在代码中插入“验证”。在执行任何操作之前,基本上检查string
中EditText
的值。
类似的东西:
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