2013-08-06 4 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를 : 여기에 우리가 글고 치기는 숫자가 포함되어 있다고 가정하고 편지가 아닌. 편지는 앱을 강제 종료합니다. 그래서 당신은 그것을 확인해야 할 것입니다.

+0

예에만 번호를 입력 할 수 게시하시기 바랍니다. 귀하의 솔루션을 nıt 작동하지 않았다. 번호가 입력 될 때 여전히 응용 프로그램이 충돌합니다. –

1

이 afterTextChanged 방법에 대한 http://developer.android.com/reference/android/text/TextWatcher.html에서 : "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

그리고 어떻게 내가 설정할 수 있습니까? –

관련 문제