1.佔位符
2.特殊符號內容的轉義
===================
以上是我本次備忘的關於Android中string.xml文件中獲取特定字符串內容使用的常見問題。
1.佔位符:
在一串字符串中希望部分內容是可變的,並且通過傳值的方式去更改內容。
舉例:
文案信息:很高興可以收到你贈送的鮮花和200金幣。
其中“鮮花”和“200金幣”是通過後臺請求Json返回的,根據不同返回內容進行替換。
常見佔位符:
%1$d 1代表第一個參數,d代表整數
%2$s 2代表第二個參數,s代表字符串
%3$.2f 3代表第三個參數,.2f代表兩位小數
除此之外: f代表浮點數,
解決方案:
1.在string.xml中保存文案信息
2.在activity中通過getString(resId,formatArgs)方法進行獲取,後面可以加多個參數
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">StringDemo</string>
<string name="hello_world">Hello world!</string>
<string name="code41_string_demo_value">很高興可以收到你贈送的%1$s和%2$d金幣。</string>
</resources>
MainActivity.java
package com.code41.demo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView demoTextView;// TextView for test string.xml
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
demoTextView = (TextView) findViewById(R.id.text_view_string_demo);
// 大部分應用都是網絡請求應用,所以要養成異步賦值的好習慣
post(new Runnable() {
@Override
public void run() {
String stringDemoValue = getString(R.string.code41_string_demo_value, "鮮花", 200);
demoTextView.setText(stringDemoValue);
}
});
}
private void post(Runnable runnable) {
if (isFinishing() || null == mHandler) {
return;
}
mHandler.post(runnable);
}
}
activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f8f8f8" >
<TextView
android:id="@+id/text_view_string_demo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#666666"
android:textSize="20sp" />
</RelativeLayout>
效果截圖:
2.轉義字符內容:
在程序開發中,Html.fromHtml是個很好用的方法,而且很多文案中會要求在同一個textView中使用多種顏色或者多種樣式。
舉例:
文案信息:很高興可以收到你贈送的鮮花和200金幣。(#eb6067 完成部分字體的顏色變化和加粗設置)
解決辦法:
取值的過程與之前的一致,在string.xml中設置的值需要加入html的樣式,同時取出後通過Html.fromHtml的方法進行轉換樣式。
使用以下方法進行轉義,但是記住獲取到的字符串含有html標籤,需要使用Html.fromHtml方法轉換成帶有樣式的Spanned設置給TextView纔可以。
<Data><![CDATA[<font color="#eb6067"><b>%1$s</b></font>和<font color="#eb6067"><b>%2$d</b></font>金幣]]></Data>
代碼:
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">StringDemo</string>
<string name="hello_world">Hello world!</string>
<string name="code41_string_demo_value">很高興可以收到你贈送的%1$s和%2$d金幣。</string>
<string name="code41_style_string_demo_value">很高興可以收到你贈送的<Data><![CDATA[<font color="#eb6067"><b>%1$s</b></font>和<font color="#eb6067"><b>%2$d</b></font>金幣]]></Data>。</string>
</resources>
MainActivity.java
package com.code41.demo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.Spannable;
import android.text.Spanned;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView demoTextView;// TextView for test string.xml
private TextView styleDemoTextView;// TextView for test string.xml
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
demoTextView = (TextView) findViewById(R.id.text_view_string_demo);
styleDemoTextView = (TextView) findViewById(R.id.text_view_style_string_demo);
// 大部分應用都是網絡請求應用,所以要養成異步賦值的好習慣
post(new Runnable() {
@Override
public void run() {
// 設置無樣式的字符串內容
String stringDemoValue = getString(R.string.code41_string_demo_value, "鮮花", 200);
demoTextView.setText(stringDemoValue);
// 設置HTML樣式的字符串內容
String styleStringDemoValue = getString(R.string.code41_style_string_demo_value, "鮮花", 200);
Spanned styleSpanned = Html.fromHtml(styleStringDemoValue);
styleDemoTextView.setText(styleSpanned);
}
});
}
private void post(Runnable runnable) {
if (isFinishing() || null == mHandler) {
return;
}
mHandler.post(runnable);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f8f8f8" >
<TextView
android:id="@+id/text_view_string_demo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#666666"
android:textSize="20sp" />
<TextView
android:id="@+id/text_view_style_string_demo"
android:layout_below="@+id/text_view_string_demo"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#666666"
android:textSize="20sp" />
</RelativeLayout>
截圖: