android 新字符,Android字符串进阶之一(特殊字符的输入)

本文按照遇到问题的思路展开:

(一)在Res下String.xml向字符串中插入“&”符号报错

如下图所示:

edbd39240b8ea8deabc918bb430f2a13.png

翻译:在对实体的引用中,实体名必须紧跟在“&”后。

查找知道:这设计到HTML语言的字符集知识:

在网页中除了可显示常见的ASCII字符和汉字外,HTML还有许多特殊字符,它们一起构成了HTML字符集。有2种情况需要使用特殊字符,一是网页中有其特殊意义的字符,二是键盘上没有的字符。 字符集HTML字符可以用一些代码来表示,代码可以有2种表示方式。即字符代码(命名实体)和数字代码(编号实体)。字符代码以&符开始,以分号结束,其间是字符名;数字代码也以&符开始,以分号结束,其间是#号加编号。示例见下图,完整的html字符集在文后Excel附件中,并非全部,仅常用。

3bc876dfb88e80df4822bd58a22ea448.png

(二)这涉及到了Android对此有专门的处理

文档中有专门说明,如下图所示:

6faf6454abf342beb2c96dc1c1e9a8a5.png

字符串可以包含风格标签(styling tag),需要注意的是:你必须转码(escaping)缩写号( apostrophe即’)和引用号(quotation mark 即”或’)。你可风格化(style)和格式化(format)字符串。

1, 对缩写号和引号的处理

文档示例如下:

179f1751f42554aa29a3fb3ee3edb002.png

正确的转码是:

A:用双引号将字符串全部圈住

B:使用转义符号“\”

错误做法:

A:不处理

错误如下图所示:

a3366e39c1dbbed6e49671bb1cae80d3.png

正确方法见上正确转码

B:使用html的字符代码代替缩写号

错误如下图所示:

bd3b2e35c5d4786e02803da831aaf763.png

对以上错误的修正:

a331e57a88b7659bf149d97bf6332ca9.png

Note:因为xml并不是完全实时编译,所以可以错误的xml语句并不当时报错,但当项目启动时,会报错。

2, 格式化字符串(format string)

即字符串中保留参数位作为模板,可以传入变量,转换。eg,SimpleDateFormat

模板为:yyyy-MM-dd,传入Date,得到1999-10-10

String.xml文件中代码如下:

Hello, %1$s! You have %2$d new messages.

Java中代码如下:

Resources res = getResources();

String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

有必要说明的是:

%:产生字面值,貌似是这个意思。

1$:代表第一个参数

2$:代表第二个参数

s:参数类型是字符串

d:参数类型是数字

Java代码如下:

3, 在字符串中使用Html标记风格符号(Html makeup)

即Html的字符代码。一步一步,终于排除到问题的可能解答处。

1. Android支持的Html元素,如下图所示:

a4819a0ecf7a3f077f24060c35d28205.png

这三个字体标签,能够直接使用,称之为“styled text”。内即为黑体字例如:

Welcome toAndroid!

但是如果将以上的“

l “<”对应的字符代码为:<

l “>”对应的字符代码为:>

如下:

Hello, %1$s! You have<b>%2$d new messages</b>.

称之为“HTML-escaped”text,因为最终文本的显示要为styled text,所以需要将Html-styled text转为 styled text,调用fromHtml()方法。代码如下:

Resources res = getResources();

String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

CharSequence styledText = Html.fromHtml(text);

因为fromHtml()方法转换的对象是html-styled,所以调用此方法之前,必须使用文本格式化(formated text)和TextUtil.htmlEncode()方法,确保所有的可能html字符已经被转码(escape)。如果代码中含有特殊字符“&”“<”等,必须调用以上方法。代码如下:

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);

测试:如果name中含有html character,不转为html-styled,会有什么情况发生。

xml中字符串format如下:

Hello, %1$s! You have<b>%2$d new messages</b>.

测试代码如下:

publicvoidonCreate(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 = newString("");

intcount =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);

}

模拟器显示如下:

cf578489d3d95fc28876ae80a401fd0b.png

2. 进入TextUtil类中,htmlEncode()方法做格式化字符代码的转换。且Android中,只接受以上五种特殊字符。代码如下:

/**

* Html-encode the string.

* @param s the string to be encoded

* @return the encoded string

*/

publicstaticString htmlEncode(String s) {

StringBuilder sb = newStringBuilder();

charc;

for(inti =0; 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);

}

}

returnsb.toString();

}

(三)源码中

有三处出现htmlEncode()方法。

如下图所示:

1c5731110c1507f220580f6880df326d.png

第一处:即上文提到的TextUtils类

第二处: TextUtils的测试类,暂时不知道有什么用处

第三处:XmlParser类

定位到代码,如下:

dfcd32136d2a05f4e897ad22534ca92f.png

向上查看代码块描述:

0155a329517c3135eefd9368fae3de41.png

显然和之上的分析吻合。这些方法是对xml的输出,输出需要标准化,即 被未来的styled text(或者是其他)准确转换convert。

(四) 流程图

cb9eacfcda95e252d473a1ff9ff64d88.png

(五)单复数的处理(string format引入的问题)

文档中说明如下:

b72219c03edbf840954a62063aa13706.png

测试各个关键字的效果如何。

Xml中定义plurals,如下:

Zero song found.

One song found.

Two song found.

Few song found.

Other song found.

Many song found.

代码中如下:

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

intcount1 =0;

Resources res = getResources();

String songsFound1 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count1, count1);

TextView textView1 = (TextView) findViewById(R.id.textView1);

textView1.setText(songsFound1);

intcount2 =1;

String songsFound2 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count2, count2);

TextView textView2 = (TextView) findViewById(R.id.textView2);

textView2.setText(songsFound2);

intcount3 =2;

String songsFound3 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count3, count3);

TextView textView3 = (TextView) findViewById(R.id.textView3);

textView3.setText(songsFound3);

intcount4 =3;

String songsFound4 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count4, count4);

TextView textView4 = (TextView) findViewById(R.id.textView4);

textView4.setText(songsFound4);

intcount5 =4;

String songsFound5 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count5, count5);

TextView textView5 = (TextView) findViewById(R.id.textView5);

textView5.setText(songsFound5);

intcount6 =1000;

String songsFound6 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count6, count6);

TextView textView6 = (TextView) findViewById(R.id.textView6);

textView6.setText(songsFound6);

}

模拟器显示如下:

3dea46fa951c9188fd54f5cebfe17deb.png

即在中文状态下,只支持one和other两个关键字。

The End!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值