0

当我在edittext中输入数字时,应用程序强制关闭。我希望应用程序在不按按钮的情况下输入值时自动计算 BMI。

我对 TextWatcher 一无所知,但我研究并想出了这段代码。它虽然不起作用。它出什么问题了?

public class BodyMassIndex extends Activity implements View.OnClickListener,
    TextWatcher {

double result;
EditText age, height, weight;
ToggleButton sex, cmin, kglb;
TextView tvResult;
double heightInt = 1;
double weightInt = 0;
int ageInt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bmi);
    initialize();

    sex.setOnClickListener(this);
    cmin.setOnClickListener(this);
    kglb.setOnClickListener(this);


}

private void initialize() {
    // TODO Auto-generated method stub

    age = (EditText) findViewById(R.id.etAge);
    height = (EditText) findViewById(R.id.etHeight);
    weight = (EditText) findViewById(R.id.etWeight);
    sex = (ToggleButton) findViewById(R.id.tbSex);
    cmin = (ToggleButton) findViewById(R.id.tbCMIN);
    kglb = (ToggleButton) findViewById(R.id.tbKGLB);
    tvResult = (TextView) findViewById(R.id.tvResult);
    age.addTextChangedListener(this);
    height.addTextChangedListener(this);
    weight.addTextChangedListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch (v.getId()) {
    case R.id.tbCMIN:
        heightInt = heightInt * 0.0254;
        break;
    case R.id.tbKGLB:
        weightInt = weightInt * 0.45359237;
        break;
    }

}

@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
    calculate();
}

private void calculate() {
    // TODO Auto-generated method stub
     Editable H = height.getText(), W = weight.getText();

    if(H != null)
        heightInt = Double.parseDouble(H.toString());
    if (W != null)
        weightInt = Double.parseDouble(W.toString());

    result = weightInt / (heightInt * heightInt);
    String textResult = "Your BMI is " + result;
    tvResult.setText(textResult);
}

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


}

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

    }
}

'

4

2 回答 2

1

来自http://developer.android.com/reference/android/text/TextWatcher.html关于 afterTextChanged 方法:"but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively....you can use setSpan(Object, int, int, int) in onTextChanged(CharSequence, int, int, int) "

于 2013-08-06T02:37:50.927 回答
0

你已经有了 :

 height = (EditText) findViewById(R.id.etHeight);
    weight = (EditText) findViewById(R.id.etWeight);

只改变 calculate() 方法:

private void calculate()
{
//remove the Editable thing declaration...
heightInt = Double.parseDouble(height.getText().toString());
wightInt = Double.parseDouble(weight.getText().toString());
result = weightInt / (heightInt * heightInt);
String textResult = "Your BMI is " + result;
tvResult.setText(textResult);

}

PS:这里我们假设 EditText 只包含一个数字而不是字母。信件将强制关闭应用程序。所以你必须检查一下。

于 2013-08-06T02:33:05.037 回答