"Go" questions about percent signs in Android resource files

Source: Internet
Author: User

 questions about percent signs in Android resource filesCategory: Android JAVA2014-08-01 16:53 1345 people read reviews (0) favorite reports Android Format resource file

Directory (?) [+]

Problem
When editing Strings.xml,


Good

[HTML]View Plaincopy
    1. <string name="Myurl">http://code.dd.com/rr? q=%rr.55</string>

Or

[HTML]View Plaincopy
    1. <string name= "Message_content '> Congratulations you earn%1s points,%%2s%</string>

Prompt for the following error

[CSS]View Plaincopy
    1. Multiple annotations found at the line:
    2. -Error:multiple substitutions specified in non-positional format; do you mean to add
    3. The formatted="false" attribute?
    4. -Error:unexpected End Tag String


The reason for this error is mainly because the strings string contains a percent percent (%),

One: There are several ways to solve 1. Use two percent sign to denote a percent semicolon [HTML]View Plaincopy
    1. <string name="Myurl">http://code.dd.com/rr? q=%%rr.55</string>
    2. <string name= "Message_content '> Congratulations you earn%1s points, account for%2s percent</string>

2. Use the escape character to indicate [HTML]View Plaincopy
    1. <string name="Myurl">http://code.dd.com/rr? q=\%rr.55</string>

Two: no formatting required

Depending on the error message, if the string does not need to be formatted, you can add the attribute on the <string label: formatted= "false", i.e.

[HTML]View Plaincopy
    1. <string name="Myurl" formatted="false">HTTP://CODE.DD.COM/RR? q=%rr.55</string>

Three: The extension of the above problem

If you need to use String.Format (String, Object ...) to format your string, you can put the formatting parameters in your string, see the following example:

[HTML]View Plaincopy
    1. <string name="Welcome_messages">hello,%1$s! You have%2$d new messages. </string>

In this example, the formatted string has 2 parameters,%1 $ s is a string%2$d is a floating-point number, you can use the following method in your program to format the string according to the parameters:

[Java]View Plaincopy
    1. Resources res = getresources ();
    2. String Text = String.Format (res.getstring (r.string.welcome_messages), username, mailcount);

So, according to the example, I need to change the%s to%1 $ s, and after the modification, the program starts successfully.

"Go" questions about percent signs in Android resource files

Related Article

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.