Android format (dynamically change) the display of string resources via String.Format

Source: Internet
Author: User

First, the realization effect:

Recently in the project need to do similar to the display of the effect, the number and title is dynamically acquired, for this kind of display effect, like the following two solutions to deal with:

(1) Dynamically set TextView content through code, such as:

/** * Show Today status *  * */private void showtodaystate (int totaluser, float uprate, String Hornor, char level) {StringBuilder Todaystatestr = new StringBuilder (); Todaystatestr.append ("You are with"); Todaystatestr.append (Totaluser); Todaystatestr.append ("Friends are playing together, this week the rankings have improved"); Todaystatestr.append (uprate); Todaystatestr.append (", obtained"); Todaystatestr.append (Hornor); Todaystatestr.append ("title, Rank"); Todaystatestr.append (level); Todaystatestr.append ( "! "); Mtodaystatetxt.settext (TODAYSTATESTR);}
then call the method:

Showtodaystate (1234567, 68.376f, "Headshot Master", ' B ');

(2) Define a string in the String.xml file, and then format the string in code with the String.Format method, for example:

<string name= "Today_state_string" > You are playing with%1$d friends, this week's ranking improved%2$.3f,%3$s title, Rank%4$c! </string>
make the following definitions in your code:

/** * Show Today status *  * */private void showtodaystate (int totaluser, float uprate, string hornor, char level) {string todays Tatestr = String.Format (Getresources (). getString (r.string.today_state_string), Totaluser, uprate, Hornor, level); Mtodaystatetxt.settext (TODAYSTATESTR);}
This article describes the second approach because it is obvious that the second approach is more convenient, especially when you need to dynamically change multiple display states.


Second, the string resource format detailed description:

<string name= "Today_state_string" > You are playing with%1$d friends, this week's ranking improved%2$.3f,%3$s title, Rank%4$c! </string>
As you can see from the definition of this line of string, the format string resource is:

The %+ parameter location +$+ The format type , where:

parameter position: starting from 1, numbering sequentially, the second parameter of the String.forma method is the first parameter formatted, followed by increments;

Type of formatting:

s string

F Floating point number

D-Integer number

C Character type

In fact, a friend of the C language know and printf print output format is similar.


Three, the attention matters:

1. When you define a formatted string resource in a String.xml file, you must ensure that the parameters inside the String.Format method are not less than the number of formatted parameters defined in the string resource (practice proves that the arguments are not reported as exceptions), such as in the example above, the string has four formatting parameter, if the code is written like this:

showtodaystate (int totaluser, float uprate, String hornor)//less level parameter
The following exception will be reported:



Iv. References:

1, modify the Strings.xml file in Android, dynamically change the data
2. Android String Resources What you don't know

Android format (dynamically change) the display of string resources via String.Format

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.