字符串编码TextView.setText()

时间:2017-11-02 20:07:53

标签: java android encoding textview

在TextView中设置文本时,字符“ù”未正确解释。这是我的代码:

TextView tv = new TextView(context);
String s;
byte[] bytes;
s = "dgseùeT41ù";
bytes = s.getBytes("ISO-8859-1");
tv.setText(new String(bytes));

我不知道我在哪里失败了。感谢您的支持

1 个答案:

答案 0 :(得分:2)

您已使用"ISO-8859-1",但java默认使用UTF-8,因此要么在创建字符串时定义字符集

From docs

  

案例映射基于由。指定的Unicode标准版本   Character类和A String表示UTF-16格式的字符串

所以

bytes = s.getBytes("ISO-8859-1");
tv.setText(new String(bytes,"ISO-8859-1"));

或根本不使用

bytes = s.getBytes();
tv.setText(new String(bytes));