复数资源
复数资源有以各种方式表示数量的字符串的列表。
例如:There is 1 student.
There are 2 students.
There are 20 students.
Android允许你将此变体表示为复数资源。
以下示例显示如何根据资源文件中的数量表示这两个变体。
There is 1 student
There are %d students
getQuantityString()方法的第一个参数是复数资源ID。第二个参数选择要使用的字符串。
当数量的值为1时,你将使用字符串。当值不为1时,必须提供第三个参数,其值将放置在%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字符串资源定义允许标准的Java字符串格式化序列。
下面的代码定义了我们可以在Java代码中使用的Java格式字符串。
此XML字符串资源文件需要位于/res/values子目录中。
simple string
Hi %2$s Java format string. %1$s again
以下代码显示如何在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>节点中使用子XML元素,例如,和其他简单的文本格式化HTML。
你可以使用此复合HTML字符串在文本视图绘制前设置文本样式。
以下XML字符串资源文件需要位于/res/values子目录中。
simple string
Hello Android, You are bold.
以下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类进行HTML编码和解码。
布局xml<?xml version="1.0" encoding="utf-8"?>
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
res/values/strings.xml<?xml version="1.0" encoding="utf-8"?>
My name is <b>%1$s</b>
Java代码/***/*www.w3cschool.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方法来做html编码。
引号字符串
我们可以在Android字符串资源文件中使用引号字符串。
引号字符串需要转义或放置在备用的引用中。
以下XML字符串资源文件需要位于/res/values子目录中。
simple string
"quoted "xyz" string"
\"double quotes\"
以下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);
从上面的代码,我们可以看到有两种方法来添加引号到字符串。
使用多个字符串资源文件
要查看在此子目录中允许的多个字符串资源文件,可以将具有以下内容的另一个文件放在同一子目录中,并调用strings1.xml。
我们可以有一个字符串资源定义如下。<?xml version="1.0" encoding="utf-8"?>
hello
hello appname
附加strings.xml文件的示例。<?xml version="1.0" encoding="utf-8"?>
hello 1
hello appname 1
Eclipse ADT插件在编译时验证这些ID的唯一性,并将它们放在R.java中作为两个附加常量:R.string.hello1和R.string.app_name1。