如何在android的XML和java代码中引用字符串常量
生活随笔
收集整理的這篇文章主要介紹了
如何在android的XML和java代码中引用字符串常量
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用字符串(string)資源
? ?? ? 在一個Android工程中,我們可能會使用到大量的字符串作為提示信息。這些字符串都可以作為字符串資源聲明在配置文件中,從而實現程序的可配置性。
? ?? ? 在代碼中我們使用Context.getString()方法,通過傳遞資源ID參數來得到該字符串,也可以在其他資源文件中引用字符串資源,引用格式為:"@string/字符串資源名稱。
? ?? ? 字符串資源XML文件的定義
? ?? ? 我們通過表來說明字符串資源是如何定義的,包括資源的位置、XML文件的格式、獲得資源的方法和引用資源的方法等。
? ?? ? 表字符串資源得定義和使用
?????? 下面將通過一個實例來演示資源文件的用法。在該實例中用到兩個字符串資源:一個在布局文件中引用;另一個在Java代碼中引用。
?
實例步驟說明如下。
? ?? ? 在該工程的res\values\目錄下,創建一個字符串資源文件stirngs.xml,內容如下所示:
Java代碼:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Test Resources</string> <string name="test_str1">從代碼中引用!</string> <string name="test_str2">從資源文件引用!</string> </resources> ?
?????? 在該工程的res\layout\目錄下,定義一個布局文件test_string.xml。在該布局文件中添加兩個TextView視圖對象:第一個TextView的文本內容直接引用strings.xml文件中的資源;第二個TextView的文本內容在代碼中設置。
Java代碼:
<?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"> <TextView android:text="@string/test_str1" android:id="@+id/myTextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="" android:id="@+id/myTextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> ?
?????? 在工程的com.amaker.ch03.string包中,創建一個TestStringActivity類。在該類的onCreate()方法中,設置當前的視圖布局,并獲得TextView實例。通過Context.getString()方法,從字符串資源中獲得字符串常量,并將其設置為TextView的文本內容。
Java代碼:
package eoe.demo.string; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import com.amaker.test.R; /* * * * @author 郭宏志 * 測試字符串資源 */ public class TestStringActivity extends Activity { private TextView myTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_string); myTextView = (TextView)findViewById(R.id.myTextView02); String str = getString(R.string.test_str2).toString(); myTextView.setText(str); } }
? ?? ? 在一個Android工程中,我們可能會使用到大量的字符串作為提示信息。這些字符串都可以作為字符串資源聲明在配置文件中,從而實現程序的可配置性。
? ?? ? 在代碼中我們使用Context.getString()方法,通過傳遞資源ID參數來得到該字符串,也可以在其他資源文件中引用字符串資源,引用格式為:"@string/字符串資源名稱。
? ?? ? 字符串資源XML文件的定義
? ?? ? 我們通過表來說明字符串資源是如何定義的,包括資源的位置、XML文件的格式、獲得資源的方法和引用資源的方法等。
? ?? ? 表字符串資源得定義和使用
| 資源位置 | res/values/strings.xml |
| 字符串XML文件 格式 | 使用<?xml version="1.0" encoding="utf-8"?> <resources>根元素 <string>子元素:<string name= color_name>string_value</string> |
| 獲得字符串資源 的方法 | Resources.getString() |
| 引用字符串資源 的格式 | Java代碼中:R.string.string_nameXML文件中:@[package:]string/string_name |
?????? 下面將通過一個實例來演示資源文件的用法。在該實例中用到兩個字符串資源:一個在布局文件中引用;另一個在Java代碼中引用。
?
實例步驟說明如下。
? ?? ? 在該工程的res\values\目錄下,創建一個字符串資源文件stirngs.xml,內容如下所示:
Java代碼:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Test Resources</string> <string name="test_str1">從代碼中引用!</string> <string name="test_str2">從資源文件引用!</string> </resources> ?
?????? 在該工程的res\layout\目錄下,定義一個布局文件test_string.xml。在該布局文件中添加兩個TextView視圖對象:第一個TextView的文本內容直接引用strings.xml文件中的資源;第二個TextView的文本內容在代碼中設置。
Java代碼:
<?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"> <TextView android:text="@string/test_str1" android:id="@+id/myTextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="" android:id="@+id/myTextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> ?
?????? 在工程的com.amaker.ch03.string包中,創建一個TestStringActivity類。在該類的onCreate()方法中,設置當前的視圖布局,并獲得TextView實例。通過Context.getString()方法,從字符串資源中獲得字符串常量,并將其設置為TextView的文本內容。
Java代碼:
package eoe.demo.string; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import com.amaker.test.R; /* * * * @author 郭宏志 * 測試字符串資源 */ public class TestStringActivity extends Activity { private TextView myTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_string); myTextView = (TextView)findViewById(R.id.myTextView02); String str = getString(R.string.test_str2).toString(); myTextView.setText(str); } }
總結
以上是生活随笔為你收集整理的如何在android的XML和java代码中引用字符串常量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 通过字符串来获取R下面资
- 下一篇: ajax 跨域请求api_【.NET C