復(fù)數(shù)資源有以各種方式表示數(shù)量的字符串的列表。
例如:
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);
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代碼。
Android允許在< string>
節(jié)點(diǎn)中使用子X(jué)ML元素,例如<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);
以下代碼顯示如何使用utilities類(lèi)進(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 <b>%1$s</b></string> </resources>
Java代碼
/***/* www.15014759268.cn */ 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
方法來(lái)做html編碼。
我們可以在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);
從上面的代碼,我們可以看到有兩種方法來(lái)添加引號(hào)到字符串。
要查看在此子目錄中允許的多個(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
。
更多建議: