From:http://mikewang.blog.51cto.com/3826268/862643
- <string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
- Resources res = getResources();
- String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
- <string name="welcome">Welcome to <b>Android</b>!</string>
- <string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
- Resources res = getResources();
- String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
- CharSequence styledText = Html.fromHtml(text);
- String escapedUsername = TextUtil.htmlEncode(username);
- Resources res = getResources();
- String text = String.format(res.getString(R.string.welcome_messages), escapedUsername, mailCount);
- CharSequence styledText = Html.fromHtml(text);
- <string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Resources rs = getResources();
- mTextView1 = (TextView) findViewById(R.id.textView1);
- mTextvView2 = (TextView) findViewById(R.id.textView2);
- String name = new String("<Mike>");
- int count = 12345;
- //未转为html-styled
- format1 = String.format(rs.getString(R.string.welcome_messages), name,count);
- CharSequence styledText1 = Html.fromHtml(format1);
- mTextView1.setText(styledText1);
- //转为html-styled
- format2 = String.format(rs.getString(R.string.welcome_messages), TextUtils.htmlEncode(name),count);
- CharSequence styledText2 = Html.fromHtml(format2);
- mTextvView2.setText(styledText2);
- }
- /**
- * Html-encode the string.
- * @param s the string to be encoded
- * @return the encoded string
- */
- public static String htmlEncode(String s) {
- StringBuilder sb = new StringBuilder();
- char c;
- for (int i = 0; i < s.length(); i++) {
- c = s.charAt(i);
- switch (c) {
- case '<':
- sb.append("<"); //$NON-NLS-1$
- break;
- case '>':
- sb.append(">"); //$NON-NLS-1$
- break;
- case '&':
- sb.append("&"); //$NON-NLS-1$
- break;
- case '\'':
- sb.append("'"); //$NON-NLS-1$
- break;
- case '"':
- sb.append("""); //$NON-NLS-1$
- break;
- default:
- sb.append(c);
- }
- }
- return sb.toString();
- }
- <plurals name="numberOfSongsAvailable">
- <item quantity="zero">Zero song found.</item>
- <item quantity="one">One song found.</item>
- <item quantity="two">Two song found.</item>
- <item quantity="few">Few song found.</item>
- <item quantity="other">Other song found.</item>
- <item quantity="many">Many song found.</item>
- </plurals>
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- int count1 = 0;
- Resources res = getResources();
- String songsFound1 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count1, count1);
- TextView textView1 = (TextView) findViewById(R.id.textView1);
- textView1.setText(songsFound1);
- int count2 = 1;
- String songsFound2 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count2, count2);
- TextView textView2 = (TextView) findViewById(R.id.textView2);
- textView2.setText(songsFound2);
- int count3 = 2;
- String songsFound3 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count3, count3);
- TextView textView3 = (TextView) findViewById(R.id.textView3);
- textView3.setText(songsFound3);
- int count4 = 3;
- String songsFound4 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count4, count4);
- TextView textView4 = (TextView) findViewById(R.id.textView4);
- textView4.setText(songsFound4);
- int count5 = 4;
- String songsFound5 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count5, count5);
- TextView textView5 = (TextView) findViewById(R.id.textView5);
- textView5.setText(songsFound5);
- int count6 = 1000;
- String songsFound6 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count6, count6);
- TextView textView6 = (TextView) findViewById(R.id.textView6);
- textView6.setText(songsFound6);
- }