【问题标题】:TextView misbehaving when appending two strings附加两个字符串时 TextView 行为异常
【发布时间】: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 符号的部分,它看起来像这样:

如果我把它放回去,我会得到这个:

我该如何解决这个问题?

【问题讨论】:

    标签: android unicode textview


    【解决方案1】:

    看起来您的 fahrenheit_symbol 和 celsius_symbol 的 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 处的字符串。
    【解决方案2】:

    改变

    return df.format(temperature) + R.string.fahrenheit_symbol;
    return df.format(temperature) + R.string.celsius_symbol;
    

    return df.format(temperature) + getString(R.string.fahrenheit_symbol);
    return df.format(temperature) + getString(R.string.celsius_symbol);
    

    R.string.fahrenheit_symbolR.string.celsius_symbol 都是整数。您需要使用Context.getResources().getString() 查找相关的字符串资源。

    您需要将Context(例如调用Activity)传递给您的ConverterUtil

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多