我的程序现在执行以下操作:
- 用户输入一个值
- 用户选择转化
- 用户点击提交
根据选择的转换(使用单选按钮),我需要调用一个方法来执行转换的计算,然后将转换返回到案例,但我遇到了问题,因为我返回的是双精度数。
package com.exercise_5;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends Activity {
private String textValue;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// Method called when the convert button is clicked
public void convert(View view) {
RadioGroup conRadioGroup = (RadioGroup) findViewById(R.id.conRadioGroup);
EditText textValue = (EditText) findViewById(R.id.editText1);
switch(conRadioGroup.getCheckedRadioButtonId()) {
case R.id.radioCelsiusToFahrenheit:
// Call the convert method
fahrenheitToCelsius();
//Return the converted variable
break;
case R.id.radioFahrenheitToCelsius:
fahrenheitToCelsius();
break;
default:
Log.e("Some class tag", "Invalid id was passed to conversion method doing celsius conversion.");
return celsiusToFahrenheit();
break;
}
}
public void fahrenheitToCelsius(Convert tempCelsius) {
double conCelsius = Double.parseDouble(textValue);
//Calculate Celsius
tempCelsius = ((conCelsius * 9) / 5) + 32;
}
public double celsiusToFahrenheit(double tempInFahrenheit) {
double conFahrenheit = Double.parseDouble(textValue);
//Calculate Fahrenheit
return ((conFahrenheit * 9) / 5) + 32;
}
public void clear(View view) {
//Reset Appended Strings After Previous Run
TextView fahrenheit_TV = (TextView) this.findViewById(R.id.textView1);
TextView celsius_TV = (TextView) this.findViewById(R.id.textView3);
fahrenheit_TV.setText("Fahrenheit: ");
celsius_TV.setText("Celsius: ");
}
}