1

我正在使用以下代码显示来自文本文件的 about 文本。但我无法正确显示德语变音符号ä、ü、ö。如何更改或设置编码?安德罗斯 说:

public InputStreamReader (InputStream in) 自:API Level 1

在 InputStream 中构造一个新的 InputStreamReader。此构造函数将字符转换器设置为“file.encoding”属性中指定的编码,如果该属性不存在,则回退到 ISO 8859_1 (ISO-Latin-1)。

public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.help); 
    TextView tv = (TextView)findViewById(R.id.help_text);
    //tv.setText(readRawTextFile(R.raw.help));
    tv.setText(Html.fromHtml(readRawTextFile(R.raw.help)));
}

public static String readRawTextFile(int id) {
    InputStream inputStream = mContext.getResources().openRawResource(id);
    InputStreamReader in = new InputStreamReader(inputStream);
    BufferedReader buf = new BufferedReader(in);
    String line;
    StringBuilder text = new StringBuilder();
    try {
        while (( line = buf.readLine()) != null) 
            text.append(line);
        //text.append("<br>" );
    } catch (IOException e) {
        return null;
    }
    return text.toString();
}

提前致谢!

4

1 回答 1

4

您可以尝试在 InputStreamReader 创建期间指定文本文件中使用的字符集:

InputStreamReader in = new InputStreamReader(inputStream, charset); 

您可以在以下位置找到可用的字符集:字符集

祝你好运。

于 2012-10-06T14:32:00.040 回答