2013-08-06 24 views
0

當我在edittext中輸入數字時,應用程序強制關閉。我希望應用程序能夠在不按按鈕的情況下輸入數值時自動計算BMI。如何使用TextWatcher自動從edittext獲取值並將它們相乘?

我對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 

    } 
} 

'

+0

'力shuts'意味着有堆棧跟蹤,請張貼堆棧跟蹤 –

回答

0

你已經有了:

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

只改變計算()方法:

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只包含了一些而不是字母。信件將強制關閉應用程序。所以你必須檢查。

+0

是唯一的號碼可以輸入。您的解決方案無法正常工作,輸入號碼後應用仍然崩潰 –

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) inonTextChanged(CharSequence的,INT,INT,INT)

+0

我該如何設置? –

相關問題