How do string.xml resources in Android add parameters?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.