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

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

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

以下图所示:api

0d2d3073727689a60eef66cd682347f2.png

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

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

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

c604ea345c110e3cf4f2aa06691848e9.png

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

文档中有专门说明,以下图所示:字体

1e5c7b3e9b8c1ac2610000111f1fc25d.png

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

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

文档示例以下:

26f9547bfa00490b94c2fd9a51e5233d.png

正确的转码是:

A:用双引号将字符串所有圈住

B:使用转义符号“\”

错误作法:

A:不处理

错误以下图所示:

625eea1fc5f5382314f55cbd66b66fa3.png

正确方法见上正确转码

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

错误以下图所示:

46b7b2e2fb4e22af683f6575af213bdc.png

对以上错误的修正:

55bf6d203b4ee61bada9b0fa148dfa2f.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元素,以下图所示:

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

}

模拟器显示以下:

2d99aeb6d5dd5227f71611fbde34ae97.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()方法。

以下图所示:

1a86d9bb9ea5b2b1fa7dcdff3acbc80b.png

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

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

第三处:XmlParser类

定位到代码,以下:

03eee4d770e1fdf94913f0b9a0341463.png

向上查看代码块描述:

6f9142ec4bfac3a1522e13989f20f2ee.png

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

(四) 流程图

6d5fbf175088b15a1160b0f4746e9e91.png

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

文档中说明以下:

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

}

模拟器显示以下:

323df82b563e95fd71b99efa583e2300.png

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

The End!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值