TextView.setText中未显示Unicode字符

Eri*_*ric 39 android

我无法让TextView正确地动态显示unicode字符,这让我感到沮丧.我已将其剥离到最低限度,但由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 );
  }
}
Run Code Online (Sandbox Code Playgroud)

这是布局:

<?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>
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

我正在用蚂蚁建造.这是我的default.properties:

target=Google Inc.:Google APIs:8
Run Code Online (Sandbox Code Playgroud)

这是我的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> 
Run Code Online (Sandbox Code Playgroud)

我已经修改了我能想到的一切,但似乎unicode字符被CharSequence接口拆分,我无法弄清楚原因.

use*_*209 67

不幸的是,你无法从strings.xml AFAIK那样做.

你还在做两件事之一.

希望这很有用.

  • 但是,某些特殊的unicode不起作用,例如\ u266B(♫) (10认同)
  • @Jacky 您需要拥有支持您的 unicode 的字体。[支持U+266B的字体](http://www.fileformat.info/info/unicode/char/266B/index.htm) (2认同)

Hei*_*erg 9

接受的答案是正确的,但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()));
    }
Run Code Online (Sandbox Code Playgroud)


Kam*_*ate 5

它可以用这么简单的方式完成:

str.replace("uoo26","&");
Run Code Online (Sandbox Code Playgroud)