Android 字符串資源格式

2018-02-18 11:46 更新

復(fù)數(shù)資源

復(fù)數(shù)資源有以各種方式表示數(shù)量的字符串的列表。

例如:

  • There is 1 student.
  • There are 2 students.
  • There are 20 students.

Android允許你將此變體表示為復(fù)數(shù)資源。

以下示例顯示如何根據(jù)資源文件中的數(shù)量表示這兩個(gè)變體。

<resources...>
   <plurals name="my_text">
      <item quantity="one">There is 1 student</item>
      <item quantity="other">There are %d students</item>
   </plurals>
</resources>

getQuantityString()方法的第一個(gè)參數(shù)是復(fù)數(shù)資源ID。第二個(gè)參數(shù)選擇要使用的字符串。

當(dāng)數(shù)量的值為1時(shí),你將使用字符串。當(dāng)值不為1時(shí),必須提供第三個(gè)參數(shù),其值將放置在%d 的位置。

Resources res = your-activity.getResources();
String s1 = res.getQuantityString(R.plurals.my_text, 0,0);
String s2 = res.getQuantityString(R.plurals.my_text, 1,1);
String s3 = res.getQuantityString(R.plurals.my_text, 2,2);
String s4 = res.getQuantityString(R.plurals.my_text, 10,10);

Java格式字符串

Android字符串資源定義允許標(biāo)準(zhǔn)的Java字符串格式化序列。

下面的代碼定義了我們可以在Java代碼中使用的Java格式字符串。

此XML字符串資源文件需要位于 /res/values 子目錄中。

<resources>
    <string name="simple_string">simple string</string>
    <string name="java_format_string">
           Hi %2$s Java format string. %1$s again
     </string>
</resources>

以下代碼顯示如何在Java代碼中使用Java格式字符串。

String simpleString = activity.getString(R.string.simple_string);
textView.setText(simpleString);

String javaFormatString = activity.getString(R.string.java_format_string);

String substitutedString = String.format(javaFormatString, "Hello" , "Android");

textView.setText(substitutedString);

java_format_string在資源xml文件中定義。我們使用Activity.getString將字符串加載到Java代碼。

HTML字符串

Android允許在< string> 節(jié)點(diǎn)中使用子XML元素,例如<b> <i> 和其他簡(jiǎn)單的文本格式化HTML。

你可以使用此復(fù)合HTML字符串在文本視圖繪制前設(shè)置文本樣式。

以下XML字符串資源文件需要位于 /res/values 子目錄中。

<resources>
    <string name="simple_string">simple string</string>
     <string name="tagged_string">
         Hello <b><i>Android</i></b>, You are bold.
     </string>
</resources>

以下Java代碼顯示如何使用html格式的字符串。

String simpleString = activity.getString(R.string.simple_string);
textView.setText(simpleString);

String htmlTaggedString = activity.getString(R.string.tagged_string);

Spanned textSpan = android.text.Html.fromHtml(htmlTaggedString);

textView.setText(textSpan);

HTML編碼和解碼

以下代碼顯示如何使用utilities類進(jìn)行HTML編碼和解碼。

布局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"
  >
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <Button android:id="@+id/format"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:text="OK"
      />
    <EditText android:id="@+id/name"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      />
  </LinearLayout>
  <TextView android:id="@+id/result"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="funky_format">My name is &lt;b&gt;%1$s&lt;/b&gt;</string>
</resources>

Java代碼

/***/* www.o2fo.com */
  Copyright (c) 2008-2009 CommonsWare, LLC
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/


import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
  EditText name;
  TextView result;
  
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    
    name=(EditText)findViewById(R.id.name);
    result=(TextView)findViewById(R.id.result);
    
    Button btn=(Button)findViewById(R.id.format);
    
    btn.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        applyFormat();
      }
    });
  }  
  private void applyFormat() {
    String format=getString(R.string.funky_format);
    String simpleResult=String.format(format,TextUtils.htmlEncode(name.getText().toString()));
    result.setText(Html.fromHtml(simpleResult));
  }
}

我們使用 TextUtils.htmlEncode 方法來做html編碼。

引號(hào)字符串

我們可以在Android字符串資源文件中使用引號(hào)字符串。

引號(hào)字符串需要轉(zhuǎn)義或放置在備用的引用中。

以下XML字符串資源文件需要位于 /res/values 子目錄中。

<resources>
    <string name="simple_string">simple string</string>
    <string name="quoted_string">"quoted "xyz" string"</string>
    <string name="double_quoted_string">\"double quotes\"</string>
</resources>

以下Java代碼顯示如何使用上面的資源文件。

String simpleString = activity.getString(R.string.simple_string);
textView.setText(simpleString);

String quotedString = activity.getString(R.string.quoted_string);
textView.setText(quotedString);

String doubleQuotedString = activity.getString(R.string.double_quoted_string);
textView.setText(doubleQuotedString);

從上面的代碼,我們可以看到有兩種方法來添加引號(hào)到字符串。

使用多個(gè)字符串資源文件

要查看在此子目錄中允許的多個(gè)字符串資源文件,可以將具有以下內(nèi)容的另一個(gè)文件放在同一子目錄中,并調(diào)用 strings1.xml 。

我們可以有一個(gè)字符串資源定義如下。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">hello</string>
    <string name="app_name">hello appname</string>
</resources>

附加 strings.xml 文件的示例。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello1">hello 1</string>
    <string name="app_name1">hello appname 1</string>
</resources>

Eclipse ADT插件在編譯時(shí)驗(yàn)證這些ID的唯一性,并將它們放在 R.java 中作為兩個(gè)附加常量: R.string.hello1 R.string.app_name1 。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)