【发布时间】:2016-03-31 15:02:20
【问题描述】:
我正在编写一个简单的温度转换程序,以熟悉 Android 编程。用户在EditText 中输入一个数字,然后它将它从华氏温度转换为摄氏温度,反之亦然,然后将答案放入TextView。我想在显示答案之前将 Unicode 摄氏/华氏符号附加到答案的末尾。当我没有它附加符号时,它可以正常工作并显示正确的数字,但是当它试图将符号附加到末尾时,输出显示全部错误,最后有一长串数字(和仍然没有 Unicode 符号)。
这是我的代码:
这是转换器实用程序类:
public class ConverterUtil {
//Convert to celsius
public static String convertFahrenheitToCelsius(float fahrenheit) {
float temperature = (fahrenheit - 32) * 5 / 9;
DecimalFormat df = new DecimalFormat("#.#");
return df.format(temperature) + R.string.celsius_symbol;
}
//Convert to fahrenheit
public static String convertCelsiustoFahrenheit(float celsius) {
float temperature = (celsius * 9) / 5 + 32; //Append the unicode Celsius symbol (\u2103), then return
DecimalFormat df = new DecimalFormat("#.#");a
return df.format(temperature) + R.string.fahrenheit_symbol; //Append the unicode Fahrenheit symbol (\u2109), then return
}
}
这就是我所说的:
public void calculateTemperature(){
RadioButton celsiusButton = (RadioButton) findViewById(R.id.button2);
TextView output = (TextView) findViewById(R.id.output);
if (text.getText().length() == 0) {
output.setText("");
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
String outputText = celsiusButton.isChecked() ? ConverterUtil.convertFahrenheitToCelsius(inputValue) : ConverterUtil.convertCelsiustoFahrenheit(inputValue);
output.setText(outputText);
}
如果我去掉我附加 Unicode 符号的部分,它看起来像这样:
我该如何解决这个问题?
【问题讨论】: