Use placeholders in Android strings

Source: Internet
Author: User

The first is that Java can be used String.format(String format, Object... args) to implement

The second is the implementation via Android getResources().getString(int id, Object... formatArgs)

Placeholder syntax can refer to the Java documentation

The second method in a simple demonstration

Strings.xml

1 <stringname= "Boolean_conversion">Boolean:%1$b\n</string>2 <stringname= "String_conversion">String:%1$s\n</string>3 <stringname= "Integer_conversion">Integer:%1$d\n</string>4 <stringname= "Float_conversion">Float:%1$.2f\n</string>5 <stringname= "Date_or_time_conversion">Year:%1$ty, Month:%1$tm, DAY:%1$TD</string>

Mainactivity.java

1  Public classMainactivityextendsactionbaractivity {2 3 @Override4         protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.ac_main);7 8TextView Text =(TextView) Findviewbyid (r.id.ac_main_text);9StringBuilder Builder =NewStringBuilder ();Ten Builder.append ( OneGetresources (). getString (R.string.boolean_conversion,true)); A  - Builder.append ( -Getresources (). getString (r.string.string_conversion, "Hello World")); the  - Builder.append ( -Getresources (). getString (R.string.integer_conversion, 1234)); -  + Builder.append ( -Getresources (). getString (R.string.float_conversion, 1234.5678)); +  ACalendar Calendar =calendar.getinstance (); at Builder.append ( - getresources (). getString (r.string.date_or_time_conversion, calendar)); -  - Text.settext (builder.tostring ()); -     } -}

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.