Android TextView.setText中未显示Unicode字符

Android TextView.setText中未显示Unicode字符,android,Android,我无法让TextView正确地动态显示unicode字符,这让我抓狂。我已经将其精简到最低限度,但是由setText填充的TextView仍然显示带有问号的菱形,其中包含unicode字符。从strings.xml填充的版本完美地显示了多字节字符。活动内容如下: public class TestMultibyteActivity extends Activity { /** Called when the activity is first created. */ @Override

我无法让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 );
  }
}
下面是布局图:

<?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>
这是我的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 str = "\u00A9" + getContext().getString(R.string.your_string);
    
  • 在java中以HTML格式输入文本:

    yourTextView.setText(Html.fromHtml("your chars"));
    

希望这是有用的。

我在试图显示Unicode专用区域中映射的自定义字体中的Unicode字符时遇到问题。这是一个常见区域,MaterialDesign和FontAwesome等字体映射其图标字体字符。为了清楚起见,我在安卓版本4.4.2上测试了这一点,所以在以后的版本中这可能不是问题(但可能是)。我尝试了“\uF151”和#xF151的所有组合,还尝试了Html.fromHtlm。这些技术都无法在Unicode专用区域中正确显示字符。如果我将自己限制在\u00xx的范围内,那么对于在这些位置具有Unicode代码的字体,它们都可以正常工作。(我提到的两种字体没有任何符号映射到这些点位置)

在使用Microsoft的字符映射检查字体时,我注意到当我为字体选择Windows英语字符集时,Unicode专用区域空间中的字符没有显示出来。如果我选择Unicode字符集,字符将显示;在材料设计字体的情况下,如果我选择了日语字符集

我所做的是将区域设置更改为日语,在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的情况下,您必须选择传统汉字集才能访问unicode专用区号点


您可能会问,为什么我没有选择“Unicode”字符集?android使用的字符集基于当前区域设置,我无法找到选择unicode作为当前区域设置的方法。此外,我找不到将私有区域unicodes映射到现有区域设置的方法。我也不能告诉你这是否是一个关于如何构建这些True Type字体的问题,或者这是否是一个Android问题(尽管我认为应该有一个为什么要映射私有区号以便在现有区域设置中使用)我学到的是,所有Unicode代码点都映射到同一字体的所有语言区域设置中。这就是我认为很多人对支持unicode的字体感到困惑的地方。windows字符映射对我来说是一个有用的工具,可以确定哪些字符点映射到哪个字符集。

可以通过以下简单方式完成:

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

被接受的答案是正确的,但是
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()));
    }

您可以使用以下代码来解决问题:

String s = String.format("hello %s", "world");
textView.setText(s);  

// expected output "hello world"

我在这里尝试了所有其他的答案,但都无法解决我的问题。最后我找到了:D

StringEscapeUtils.unescapeJava("Your unicode string")

如果要显示unicode中的文本

  • 第一步,从unicode中删除“\u”。例如:\u00A9应该是“00A9”
  • 然后,将“00A9”转换为整数。例:瓦尔·赫克瓦尔= 整数.parseInt(“00A9”,16)
  • 最后,将整数转换为字符。val char=hexVal.toChar()。然后 显示字符
  • 源代码:

     val builder = StringBuilder()
    listUnicodes.forEach 
     { uniCode ->
       val hexVal = Integer.parseInt(uniCode, 16)
       val char = hexVal.toChar()
       builder.append(char)        
    }
    return builder.toString()
    

    欢迎来到科特林世界

    如果有人希望以编程方式设置UNICODE,可以通过以下方式进行设置


    @Jacky的Unicode字符您需要具有支持Unicode的字体。这并不能回答问题。
    StringEscapeUtils.unescapeJava("Your unicode string")
    
     val builder = StringBuilder()
    listUnicodes.forEach 
     { uniCode ->
       val hexVal = Integer.parseInt(uniCode, 16)
       val char = hexVal.toChar()
       builder.append(char)        
    }
    return builder.toString()