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 {@ Override protected 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 );}}
<? 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>
<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>