In Android development, we typically use string.xml resources to set strings for controls such as TextView. The value is generally independent of the running result of the program.
But sometimes it needs to be displayed to the control based on the result of the run, when the string resource cannot be written dead.
You can use the following notation to set:
- %n$ms:s represents the output of a string, n is the number of arguments, and setting the value of M can place a space before the output. For example:%1$2s represents the string resource's 1th string argument with two spaces after the argument.
- The%N$MD:D represents the output of an integer, n is the number of arguments, and the value of M sets the space before the output can be placed.
- %n$mf:f represents the output of floating-point numbers, N is the number of parameters, and setting the value of M can control the decimal digits. such as:%2$.2f at this time m=.2, the output format is: integer part. 00
Here's how it's defined and set up:
String.xml Code
< resources > < string name = "Cart_cost" > total:%1$.2f yuan </ string > < string name = "Cart_allcount" > total:%1$1d items </ string > </ resources >
Java code
String temp = getresources (). getString (R.string.cart_cost);
The corresponding parameters are set in sequence such as: Set n values string.format (temp,param1,param2,..., paramn);
= String.Format (temp, (float) totalpiece);
You want to reset the value to the control, otherwise it won't work.
Mtotalcast.settext (piece);
Operation Result:
can be found without reset, not displayed correctly.
How do string.xml resources in Android add parameters?