android犯错日记5

情况:

在string.xml里写项目

<string name="current_question_in_total_str">%s/%s 题</string>

时出错,提示

Multiple annotations found at this line:
	- error: Multiple substitutions specified in non-positional format; did you mean to add the 
	 formatted="false" attribute?
	- error: Unexpected end tag string

这样写本意是想利用String.format(String, Object)来格式化字符串。

解决:

百度和查文档得知,如果作为格式化字符串的话,写法应该为

Formatting strings

If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource. For example, with the following resource:

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguments from your application like this:

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


例子中
Hello, %1$s! You have %2$d new messages.
有两个参数,第1个为%s,第2个为%d
猜想数字1、2对应参数号
如果不想让文本作为格式化字符串,则需添加参数
formatted="false"


<string name="test" formatted="false">%test%</string>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值