Android 字符串资源格式

2018-02-18 11:46 更新

复数资源

复数资源有以各种方式表示数量的字符串的列表。

例如:

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

Android允许你将此变体表示为复数资源。

以下示例显示如何根据资源文件中的数量表示这两个变体。

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

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 子目录中。

<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> 节点中使用子XML元素,例如<b> <i> 和其他简单的文本格式化HTML。

你可以使用此复合HTML字符串在文本视图绘制前设置文本样式。

以下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类进行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.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 子目录中。

<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);

从上面的代码,我们可以看到有两种方法来添加引号到字符串。

使用多个字符串资源文件

要查看在此子目录中允许的多个字符串资源文件,可以将具有以下内容的另一个文件放在同一子目录中,并调用 strings1.xml

我们可以有一个字符串资源定义如下。

<?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插件在编译时验证这些ID的唯一性,并将它们放在 R.java 中作为两个附加常量: R.string.hello1 R.string.app_name1

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号