Unicode字符不显示在TextView.setText中

我不能得到一个TextView来正确dynamic显示Unicode字符,这是驱动我batty。 我已经将其剥离到最低限度,但由setText填充的TextView仍然显示其中的Unicode字符的问号内的钻石。 从strings.xml填充的版本完美地显示了多字节字符。 这是活动:

public class TestMultibyteActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); setContentView( R.layout.main ); TextView textField = (TextView) findViewById( R.id.text_field ); String str = "Tübingen systemportefølje"; Log.d( "MULTIBYTE", str ); //note that this prints the multibyte chars correctly. //EDIT: oh crap, no it doesn't. might be onto something here... textField.setText( str ); } } 

这里是布局:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/text_field" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/unicodechars"/> </LinearLayout> 

这里是strings.xml:

 <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TestMultibyteActivity</string> <string name="unicodechars">Tübingen systemportefølje</string> </resources> 

我正在用antbuild造。 这是我的default.properties:

 target=Google Inc.:Google APIs:8 

这是我的AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mycompany.android.multibyte" android:versionCode="1" android:versionName="1.0"> <application android:label="@string/app_name" android:icon="@drawable/icon"> <activity android:name="TestMultibyteActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

我用我能想到的所有东西来修饰,但是好像unicode字符被CharSequence接口分裂了,我找不到原因。

不幸的是,你只能从strings.xml AFAIK中这样做。

你只剩下两件事中的一件。

  • 将java中的Unicode字符添加到XML文件中的string中:

String str =“\ u00A9”+ getContext()。getString(R.string.your_string);

  • 在java中input文本为HTML:

yourTextView.setText(Html.fromHtml(“your chars”));

希望这是有用的。

这可以用一个简单的方法来完成:

 str.replace("uoo26","&"); 

我尝试从Unicode专用区域中映射的自定义字体显示Unicode字符时遇到问题。 这是一个常见的领域,像MaterialDesign和FontAwesome字体映射其图标字体字符。 为了清晰起见,我正在testing这个在Android 4.4.2版本,所以也许这不是在更高版本(但可能是)的问题。 我曾尝试过所有“\ uF151”,&#xF151的组合,而且我也尝试了Html.fromHtlm。 这些技术都不能正确显示Unicode专用区域中的字符。 如果我保持自己的范围\ u00xx,他们都工作得很好,在那些地方有Unicode代码的字体。 (我提到的两种字体没有任何符号映射到这些点位置)

在使用Microsoft的字符映射检查字体时,我注意到Unicode专用区域空间中的字符没有出现,当我为字体select了Windows-English字符集时。 如果我select了Unicode字符集,字符会显示出来; 如果我select了日文字符集,则使用“材料devise”字体。

我在TextView中显示的字符是将Locale更改为Japanese,在Textview对象上调用setText,将正确的专用区域unicode值传递给该字符,然后正确显示该字符。 所以这里是我用来设置图标字体符号,然后恢复到英语的代码:

  TextView tv = (TextView)findViewById(R.id.myTextViewObject); Typeface tf = Typeface.createFromAsset(this.getAssets(), "material_design.ttf"); tv.setTypeface(tf); String x = "\uE693"; tv.setText(x); setDefaultLocale(getApplicationContext(),"en"); tv.append("hello"); 

这里是setDefaultLocale方法的代码:

 public static void setDefaultLocale(Context context, String locale) { Locale locJa = new Locale(locale.trim()); Locale.setDefault(locJa); Configuration config = new Configuration(); config.locale = locJa; context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); locJa = null; config = null; } 

在FontAwesome的情况下,您必须select传统中文字符集才能访问unicode专用区域代码点。

你可能会问,为什么我没有select“Unicode”字符集? android使用的字符集是基于当前的语言环境,我找不到一种方法来selectunicode作为当前的语言环境。 另外,我找不到将私人区域的Unicode映射到我现有的区域设置的方法。 我也不能告诉你,这是否是这些TrueType字体在哪里构造的问题,或者这是否是一个Android问题(尽pipe我认为应该有一个为什么映射专用区域代码在现有的区域设置中使用)我所学到的是所有的Unicode代码点都被映射到相同字体的所有语言区域。 这是我认为很多人对支持unicode的字体感到困惑。 窗口字符映射对于我确定哪些字符点映射到哪个字符集是一个有用的工具。

接受的答案是正确的,但Html.fromHtml已被弃用。 所以你需要使用:

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { textView.setText(Html.fromHtml(user.getInput(), Html.FROM_HTML_MODE_LEGACY)); } else { textView.setText(Html.fromHtml(user.getInput())); }