Mainactivity is as follows:
Package CN. test; import android. OS. bundle; import android. app. activity;/*** demo Description: * in the resource file strings. use a placeholder * in a string in XML and replace it with */public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init ();} private void Init () {string stringstart = getresources (). getstring (R. string. stringstart); string startresult = string. format (stringstart, "Placeholder test:"); system. out. println ("startresult =" + startresult); string stringtest = getresources (). getstring (R. string. stringtest); string stringresult = string. format (stringtest, "Stephen Chow", "Hong Kong", 55); system. out. println ("stringresult =" + stringresult); string numbertest = getresources (). getstring (R. string. numberdata); string numberresult = string. format (numbertest, 9527, 88.88f); system. out. println ("numberresult =" + numberresult );}}
Strings. XML is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "app_name"> teststringresource </string> <string name = "action_settings"> Settings </string> <string name = "hello_world"> Hello world! </String> <string name = "stringstart"> Start now % 1 $ S </string> <string name = "stringtest"> my name is % 1 $ s, I am from % 2 $ S, I am % 3 $ D years old </string> <! --. 3f indicates a floating point number that retains three decimal places --> <string name = "numberdata"> my number is (integer type): % 1 $ D, and my salary is (floating point type): % 2 $. 3f </string> </resources>
Main. XML is as follows:
<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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="25sp" android:text="@string/hello_world" /></RelativeLayout>