Android 文本视图在附加两个字符串时出现错误
我正在写一个简单的温度转换程序,以熟悉Android编程。用户在编辑文本中键入一个数字,然后将其从华氏温度转换为摄氏温度,反之亦然,然后将答案放入文本视图中。在显示答案之前,我想在答案的末尾附加一个Unicode摄氏度/华氏度符号。当我没有让它附加符号时,它工作正常并显示正确的数字,但当它试图将符号附加到末尾时,输出显示完全错误,末尾有一长串数字(仍然没有Unicode符号) 这是我的密码: 这是转换器实用程序类:Android 文本视图在附加两个字符串时出现错误,android,unicode,textview,Android,Unicode,Textview,我正在写一个简单的温度转换程序,以熟悉Android编程。用户在编辑文本中键入一个数字,然后将其从华氏温度转换为摄氏温度,反之亦然,然后将答案放入文本视图中。在显示答案之前,我想在答案的末尾附加一个Unicode摄氏度/华氏度符号。当我没有让它附加符号时,它工作正常并显示正确的数字,但当它试图将符号附加到末尾时,输出显示完全错误,末尾有一长串数字(仍然没有Unicode符号) 这是我的密码: 这是转换器实用程序类: public class ConverterUtil { //Conv
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符号的部分,它看起来如下所示:
如果我把它放回去,我会得到这个:
我如何解决这个问题?更改
return df.format(temperature) + R.string.fahrenheit_symbol;
return df.format(temperature) + R.string.celsius_symbol;
到
R.string.fahrenheit_symbol
和R.string.Celsiu symbol
都是整数。您需要使用Context.getResources().getString()
查找相关的字符串资源
您需要将上下文
(例如调用活动
)传递给您的转换器直到更改
return df.format(temperature) + R.string.fahrenheit_symbol;
return df.format(temperature) + R.string.celsius_symbol;
到
R.string.fahrenheit_symbol
和R.string.Celsiu symbol
都是整数。您需要使用Context.getResources().getString()
查找相关的字符串资源
您需要将上下文
(例如调用活动
)传递给转换器,直到看起来您的华氏度符号和摄氏度符号的资源ID被附加到文本中,而不是实际的字符
试试这个
public class ConverterUtil {
//Convert to celsius
public static String convertFahrenheitToCelsius(Context context, float fahrenheit) {
float temperature = (fahrenheit - 32) * 5 / 9;
DecimalFormat df = new DecimalFormat("#.#");
return df.format(temperature) + context.getResources().getString(R.string.celsius_symbol);
}
//Convert to fahrenheit
public static String convertCelsiustoFahrenheit(Context context, 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) + context.getResources().getString(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(YourActivity.this, inputValue) : ConverterUtil.convertCelsiustoFahrenheit(YourActivity.this, inputValue);
output.setText(outputText);
}
看起来您的华氏符号和摄氏符号的resourceID被附加到文本中,而不是实际字符
试试这个
public class ConverterUtil {
//Convert to celsius
public static String convertFahrenheitToCelsius(Context context, float fahrenheit) {
float temperature = (fahrenheit - 32) * 5 / 9;
DecimalFormat df = new DecimalFormat("#.#");
return df.format(temperature) + context.getResources().getString(R.string.celsius_symbol);
}
//Convert to fahrenheit
public static String convertCelsiustoFahrenheit(Context context, 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) + context.getResources().getString(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(YourActivity.this, inputValue) : ConverterUtil.convertCelsiustoFahrenheit(YourActivity.this, inputValue);
output.setText(outputText);
}
真酷,成功了!那这是怎么回事?什么是context.getResources().getString()
?在您的项目中将生成一个R.java
文件,项目中的所有资源都被分配了一个唯一的整数资源ID。您可以使用getString(int)或getText(int)来检索此资源ID处的字符串。好极了,成功了!那这是怎么回事?什么是context.getResources().getString()
?在您的项目中将生成一个R.java
文件,为项目中的所有资源分配一个唯一的整数资源ID。您可以使用getString(int)或getText(int)来检索此资源ID处的字符串。