当EditText为空并单击calculateButton时,应用程序崩溃

时间:2014-03-12 15:33:52

标签: android

我有2个editText(weightT和heightT)...如果我单击calculateButton我的应用程序崩溃...如果2 editText为空,有什么方法可以禁用calculateButton?或者用户没有输入editText中的一个..或弹出消息"请输入数字" ...类似的东西......请帮助......

这是我的java代码:

  public class BMIActivity extends Activity {

         @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_bmi);

       }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.bmi, menu);
    return true;
}

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 + ".");}


        private int calculateBMI (int weight, int height) {

    return (int)  weight * 703 / (height * height) ;

        }

3 个答案:

答案 0 :(得分:1)

按下计算按钮时,检查两个文本字段的内容。如果其中一个包含空字符串,则可以简单地返回打破执行流程

public void calculateClickHandler(View view) {
    EditText weightText = (EditText) findViewById(R.id.weightT);
    EditText heightText = (EditText)findViewById(R.id.heightT);
    String weightString = weightText.getText().toString();
    String heightString = heightText.getText().toString();
    if (view.getId() == R.id.calculateButton) {
         if (TextUtils.isEmpty(weightString) || TextUtils.isEmpty(heightString)) {
               // show error; 
               return;
         }
    }

     //... other code ... 
}

答案 1 :(得分:0)

  如果2 editText为空,有没有办法禁用calculateButton?

不确定。您可以使用TextWatcher并根据字符的长度禁用/启用ButtonThis answer has an example of doing this

  

或者用户没有输入editText ..或弹出消息"请输入数字"

再次确定。这可能会更容易。包裹try/catch并根据TextViews的值显示相应的消息。像

这样的东西
try
{
    int weight = (int) Float.parseFloat(weightText.getText().toString());
    int height = (int) Float.parseFloat(heightText.getText().toString());
    int bmiValue = calculateBMI(weight, height);
}
catch (NumberFormatException e)
{   // display appropriate message (i.e. Toast)    }

答案 2 :(得分:0)

执行此操作的好方法是将textWatchers添加到editTexts,

weightText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(weightText.length < 1)
                    (Button)findViewById(R.id.yourcalculatebutton).setEnabled(false);
                else
                    (Button)findViewById(R.id.yourcalculatebutton).setEnabled(true);
            }
        });

就是其中之一。