600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 自学笔记:setTag getTag

自学笔记:setTag getTag

时间:2021-04-30 09:41:43

相关推荐

自学笔记:setTag getTag

1.设置tag key的唯一性

官方的api文档:“ The specified key should be an id declared in the resources of the application to ensure it is unique (see the ID resource type). Keys identified as belonging to the Android framework or not associated with any package will cause anIllegalArgumentExceptionto be thrown.”

所以抛出IllegalArgumentException的原因就在于key不唯一,那么如何保证这种唯一性呢?定义一个final类型的int变量和硬编码一个值的方式都是行不通的。

如果只需要设置一个tag,那么直接调用setTag(Object tag)方法就可以轻松搞定,如果一定需要使用多个tag绑定,那么需要先在res/values/strings.xml中添加

<resources>

<item type="id" name="tag_first"></item><item type="id" name="tag_second"></item>

</resources>

使用的时候写成

p_w_picpathView.setTag(R.id.tag_first,"Hello");p_w_picpathView.setTag(R.id.tag_second,"Success");

就可以了

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。