我想我对这里的活动生命周期感到困惑,我正在尝试执行简单的权重转换,没什么了不起的。
用户输入一个值,选择他们是否希望将其转换为 lbs 或 kgs,当他们按下“Convert”时,它应该吐出转换后的值,查看堆栈跟踪似乎与使用的 parseInt 有问题。
public class WeightConverter extends Activity implements OnClickListener, OnCheckedChangeListener {
Button convertWeight;
TextView conversionResults;
EditText enterWeight;
RadioGroup weightPicker;
RadioButton radKG, radLB;
double weightValue, convertedWeight;
String weightString;
String measurement;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.weightconverter);
initializeVariables();
}
private void initializeVariables() {
convertWeight = (Button) findViewById(R.id.btnConvertWeight);
conversionResults =(TextView) findViewById(R.id.tvWeightConversion);
enterWeight =(EditText) findViewById(R.id.etWeightToConvert);
weightPicker = (RadioGroup) findViewById(R.id.rgWeightType);
radKG = (RadioButton) findViewById(R.id.radKG);
radLB = (RadioButton) findViewById(R.id.radLB);
convertWeight.setOnClickListener(this);
}
@Override
public void onClick(View v) {
;
switch (v.getId()) {
case R.id.btnConvertWeight:
weightValue = Double.parseDouble(enterWeight.getText().toString());
conversionResults.setText(weightValue + " = " + weightString + measurement);
break;
}
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radKG:
convertedWeight = weightValue / 2.2;
measurement = " Kilograms";
break;
case R.id.radLB:
convertedWeight = weightValue * 2.2;
measurement = " Pounds";
break;
}weightString = String.valueOf(convertedWeight);
}
}