2011-05-19 45 views
14

有沒有什麼方法可以在我的手機上顯示TextView中的所有Unicode字符?在TextView中顯示所有的Unicode字符

我嘗試過特殊字符,比如'\ u0279',我得到了一些像box(默認字符)的東西。 這是基於l10n和i18n設置嗎?

+0

你是否在Strings.xml文件中添加這些字符? – mudit 2011-05-19 06:55:21

+0

假設我想從(utf8)數字(例如0279)到(utf8)char'\ u0279'進行簡單的轉換。所以我有EditText用戶寫下數字和TextView,其中這些數字被轉換爲字符。通過按下按鈕轉換完成。 – zmeda 2011-05-19 07:02:38

+0

爲此,您必須記錄各個符號的所有可用字符代碼。將它與記錄進行比較,然後顯示符號。 – mudit 2011-05-19 07:05:59

回答

14
tv=(TextView)findViewById(R.id.textView1); 
Typeface font= Typeface.createFromAsset(getAssets(), "TAU_BHON.TTF"); 
tv.setTypeface(font); 

地方的字體,會支持你的語言在folder.In這種情況下,我已經使用TAU_BHON.TTF資產

+1

我收到錯誤java.lang.RuntimeException:無法啓動活動ComponentInfo {mypackage/mypackage.MyActivity}:java.lang.RuntimeException:無法創建本機字體 – zmeda 2011-05-19 07:53:01

+1

我已成功從資產中加載字體,但仍然收到這個醜陋的盒子,而不是適當的字符。 – zmeda 2011-05-19 11:32:24

+0

你是如何確定你需要哪種字體的? – AdamMc331 2015-06-17 18:04:56

10

試試這個代碼:

String str = "\u00F6"; 

System.out.println("new value-" + str); 

您將獲得:

new value-ö 

這是我創建的示例Android項目來檢查功能:

的Java文件:

package com.testd; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class Main extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     String str = "\u00F6"; 

     TextView tv = (TextView) findViewById(R.id.a); 
     tv.setText("sajklasdklfjasdf " + str); 
    } 
} 

XML文件:

<?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/a" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:text="@string/hello" /> 
</LinearLayout> 

屏幕截圖:

enter image description here

+0

這一切都是真實的,但問題是我的TextView沒有以這種方式顯示它。它使用方塊而不是特性字符 – zmeda 2011-05-19 07:40:51

+1

行'String s = new String(str);'完全沒有什麼效率。它只是創建一個新的'String',其**內容**與's'相同。在只有一個'String'的String構造函數很有用的情況下,很少有這種情況。這不是其中的一個。 – 2011-05-19 08:58:33

+0

你是對的!編輯我的代碼。 – mudit 2011-05-19 09:00:19

12

這通常是因爲字體使用,默認的一個不支持/已經實現了所有的Unicode字符,你需要使用一個全功能的字體像DejaVuSans.ttf

@sakthi表明,應該沒有問題工作的樣本,請檢查您的Android版本支持

+0

正如我所提到的,我得到錯誤java.lang.RuntimeException:無法啓動活動ComponentInfo {mypackage/mypackage.MyActivity}:java.lang.RuntimeException:本機字體無法制作 – zmeda 2011-05-19 10:58:31

+0

rigth,但該示例說:字體font = Typeface.createFromAsset(getAssets(),「TAU_BHON.TTF」);嘗試,而不是隻getAssests()類似getContext()。getAssests(),也許是因爲無法獲得上下文 – yeradis 2011-05-19 11:30:13

+0

我已成功從資產中加載字體,仍然我得到這個醜陋的框,而不是正確的字符。 – zmeda 2011-05-19 11:33:07