天天看點

Android字元串格式化輸出

     轉載自: http://blog.csdn.net/wsywl/article/details/6555959

    在Android項目布局中,資源以XML檔案的形式存儲在res/目錄下。為了更好的實作國際化及本地化,字元串集通常以XML檔案的形式存儲在res/values/目錄下。

1、純文字字元串

      一般來說,使用純文字字元串僅僅需要res/values目錄下的一個XML檔案(通常命名為res/values/strings.xml,可以使用其它的檔案名替換strings),根元素為resources,希望編碼為資源的每個字元串都有一個string子元素。String元素包含name特性,它标示了此字元串的唯一名稱,還有一個文本元素,包含字元串的文本。

字元串的表示分以下三種情況:

a) 普通字元串(不含雙引号(”)及單引号(’))。其在XML檔案中如代碼一所示定義。

代碼一:

[cpp] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World!</string>  
  4. </resources>  

b) 字元串僅含單引号。其在XML檔案中如代碼二或代碼三(使用轉義字元反斜杠“/”)所示定義。

代碼二:

[cpp] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3. <string name="hello">"Hello' World!"</string>  
  4. </resources>  

代碼三:

[cpp] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello/' World!</string>  
  4. </resources>  

c) 其它情況下的字元串。其在XML檔案中如代碼三所示定義,即使用一個前置反斜杠進行轉義。代碼四給出了一個示例。

代碼四:

[cpp] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello/' World/"!</string>  
  4. </resources>  

2、格式字元串

      Android與Java的其它實作一樣支援格式字元串。這裡字元串包含一些占位符,表示在運作時要使用可變資訊替換的資料(例如,Hello everyone, my name is %1$s)。占位符的規定簡述如下:其使用%[index]$[type]格式進行标記,index标記替換資源中第index個資源對應的位置,type則标示所要替換的資源的類型(s表示資源為字元串格式)。這裡給出一個格式字元串的例子,代碼五為檔案strings.xml中的内容,代碼六為進行字元串替換時的Java代碼,圖1則給出了最終的效果圖。

代碼五:

[cpp] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, my name is %1$s!</string>  
  4.     <string name="app_name">MyString</string>  
  5. </resources>  

代碼六:

[cpp] view plain copy

  1. TextView mytext = (TextView)findViewById(R.id.mystring);  
  2. String myname = getString(R.string.hello);  
  3. myname = String.format(myname, "clark");  
  4. mytext.setText(myname);  

圖1:

Android字元串格式化輸出

3、樣式字元串

      Android中可以使用<b>、<i>及<u>的輕量級HTML标記對字元串進行樣式處理。有如下幾種方法進行字元串的樣式化。

a) 直接将HTML标記寫入字元串資源中,同時在布局檔案中直接引用。這裡給出一個例子,代碼七為檔案strings.xml中的内容,代碼八為布局檔案的内容,代碼九則給出了Activity中onCreate函數中的内容,圖2則給出了最終的效果圖。這種情況下,能使用的HTML标記僅為<i>、<b>及<u>三種。

代碼七:

[cpp] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello"><b>Hello World,</b> <i>my name is</i> <u>clark</u>!</string>  
  4.     <string name="app_name">MyString</string>  
  5. </resources>  

代碼八:

[cpp] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:id="@+id/mystring"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="@string/hello"  
  12.     />  
  13. </LinearLayout>  

代碼九:

[cpp] view plain copy

  1. public void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     setContentView(R.layout.main);  
  4. }  

圖2:

Android字元串格式化輸出

b) 字元串資源檔案中使用轉義HTML标記,同時使用Java代碼對字元串資源進行轉化。這裡給出一個例子,代碼十為檔案strings.xml中的内容,代碼十一為布局檔案的内容,代碼十二則給出了Activity中onCreate函數中的内容,其效果如圖2所示。

代碼十:

[cpp] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello"><b>Hello World,</b> <i>my name is</i> <u>clark</u>!</string>  
  4.     <string name="app_name">MyString</string>  
  5. </resources>  

代碼十一:

[cpp] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:id="@+id/mystring"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text=""  
  12.     />  
  13. </LinearLayout>  

代碼十二:

[cpp] view plain copy

  1. public void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     setContentView(R.layout.main);  
  4.     TextView mytext = (TextView)findViewById(R.id.mystring);  
  5.     String myname = getString(R.string.hello);  
  6.     Spanned textspan = Html.fromHtml(myname);  
  7.     mytext.setText(textspan);  
  8. }  

c) 直接将HTML标記寫入字元串資源中,同時使用Java代碼對字元串資源進行轉化。這裡給出一個例子,代碼七為檔案strings.xml中的内容,代碼十一為布局檔案的内容,代碼十三則給出了Activity中onCreate函數中的内容,其效果如圖2所示。

代碼十三:

[cpp] view plain copy

  1. public void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     setContentView(R.layout.main);  
  4.     TextView mytext = (TextView)findViewById(R.id.mystring);  
  5.     mytext.setText(getResources().getText(R.string.hello));  
  6. }  

d) 字元串資源檔案中使用純文字字元串,同時使用Java代碼對字元串資源進行轉化。這裡給出一個例子,代碼十四為檔案strings.xml中的内容,代碼十五為布局檔案的内容(注意,界面顯示的是EditText),代碼十六則給出了Activity中onCreate函數中的内容,其效果如圖3所示。這裡在定義字元串的時候也可以将其直接定義在Java代碼中。

代碼十四:

[cpp] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, my name is clark!</string>  
  4.     <string name="app_name">MyString</string>  
  5. </resources>  

代碼十五:

[cpp] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <EditText    
  8.     android:id="@+id/mystring"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text=""  
  12.     />  
  13. </LinearLayout>  

代碼十六:

[cpp] view plain copy

  1. public void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     setContentView(R.layout.main);  
  4.     EditText mytext = (EditText)findViewById(R.id.mystring);  
  5.     mytext.setText(R.string.hello);  
  6.     Spannable spn = mytext.getText();  
  7.     spn.setSpan(new BackgroundColorSpan(Color.GRAY), 0, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  8.     mytext.setText(spn);  
  9. }  

圖3:

Android字元串格式化輸出

4、樣式字元串格式化

對樣式字元串進行格式化,有以下幾個步驟:

a) 對字元串資源中的HTML标記進行轉義。例如,&lt;b&gt;Hello World,&lt;/b&gt; &lt;i&gt;my name is&lt;/i&gt; &lt;u&gt;%1$s&lt;/u&gt;!

b) 按一般情況檢索字元串資源。例如,getString(R.string.hello)。

c) 生成格式結果,確定轉義你替換的任何字元串值,防止它們包含尖括号或&符号。例如,String.format(getString(R.string.hello),TextUtils.htmlEncode(name))。

d) 通過Html.fromHtml()将實體轉義的HTML轉換為Spanned對象。例如,Spanned textspan = Html.fromHtml(myname)。

這裡給出一個例子,代碼十七為檔案strings.xml中的内容,代碼十一為布局檔案的内容,代碼十八則給出了Activity中onCreate函數中的内容,其效果如圖2所示。

代碼十七:

[cpp] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello"><b>Hello World,</b> <i>my name is</i> <u>%1$s</u>!</string>  
  4.     <string name="app_name">MyString</string>  
  5. </resources>  

代碼十八:

[cpp] view plain copy

  1. public void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     setContentView(R.layout.main);  
  4.     TextView mytext = (TextView)findViewById(R.id.mystring);  
  5.     String myname = getString(R.string.hello);  
  6.     myname = String.format(myname, "clark");  
  7.     Spanned textspan = Html.fromHtml(myname);  
  8.     mytext.setText(textspan);  
  9. }  

5、參考内容

(1)、http://book.douban.com/subject/5353163/

(2)、http://baike.baidu.com/view/5626871.html?fromTaglist