In the project, as little as possible in the code in Chinese (except annotations), in this, you need to set the string to String.xml
General usage
EXAMPLE 1:
String.xml in configuration: <string name= "Hello" >Hello!</string>
The layout XML is used in the following way: <textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"/>
Code is used in the following way: string string = GetString (R.string.hello); /String string = Getresources (). getString (R.string.hello);
Of course, it is also possible to set the string array in String.xml
EXAMPLE 2:
String.xml in configuration: <string-array name= "Planets_array" >
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
Code is used in the following ways: Resources res = getresources ();
String[] Planets = Res.getstringarray (R.array.planets_array);
Note that special characters are used in the configuration file, eg: single quotation mark "\" in English state, newline "\ n", percent "\%", "\@" symbol
EXAMPLE 3:
String.xml in configuration: correct: <string name= "Good_example" > "This ll work" </string>--in double quotes just as String output
<string name= "good_example_2" >this\ ' ll also work</string>--without double quotes, you need to consider the escape character
<string name= "Good_example_3" formatted= "false" >this ' ll also work</string>--no double quotes, set cancel formatting
Error: <string name= "bad_example" >this doesn ' t work</string>
<string name= "bad_example_2" >xml encodings don't work</string>
Of course, wildcard methods are also supported in String.xml
EXAMPLE 4:
String.xml in configuration: <string name= "Welcome_messages" >hello,%1$s! You have%2$d new messages.</string>
% 1,% 2 represents the order of the parameters, $s represents the output string, $d represents the output decimal integer, $f represents the output decimal floating point number
Code is used in the following ways: Resources res = getresources ();
String Text = String.Format (res.getstring (r.string.welcome_messages), username, mailcount);
Finally, a string with HTML style
EXAMPLE 5:
String.xml in configuration: <string name= "Welcome_messages" >hello,%1$s! You have <b>%2$d new messages</b>.</string>
It is important to note that the escape character is used,eg:< or &, and example 3 can be referenced.
Code is used in the following ways: Resources res = getresources ();
String Text = String.Format (res.getstring (r.string.welcome_messages), username, mailcount);
Charsequence Styledtext = html.fromhtml (text);
It is important to note that the parameter 1:username, if the username contains special characters < or &, you need to escape the special character
String escapedusername = Textutil.htmlencode (username);
Resources res = getresources ();
String Text = String.Format (res.getstring (r.string.welcome_messages), Escapedusername, Mailcount);
Charsequence Styledtext = html.fromhtml (text);
According to the official document of the introduction to here, there is a lack of a special character quantity string usage, do not see the formal documentation of the description is less used, ignore it
Android r.string