android setTag (int key, Object tag)使用

简介: android setTag (int key, Object tag)使用

setTag是android的view类中很有用的一个方法,可以用它来给空间附加一些信息,在很多场合下都得到妙用。

setTag(Object tag)方法比较简单,这里主要谈一谈带两个参数的setTag方法。

官方的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 an IllegalArgumentExceptionto be thrown.”所以抛出IllegalArgumentException的原因就在于key不唯一,那么如何保证这种唯一性呢?定义一个final类型的int变量和硬编码一个值的方式都是行不通的。

private static final int TAG_ONLINE_ID = 1;((Button) row.findViewById(R.id.btnPickContact)).setTag(TAG_ONLINE_ID,objContact.onlineid);05-18 20:29:38.044: ERROR/AndroidRuntime(5453): java.lang.IllegalArgumentException: The key must be an application-specific resource id.
05-18 20:29:38.044: ERROR/AndroidRuntime(5453):     at android.view.View.setTag(View.java:7704)
05-18 20:29:38.044: ERROR/AndroidRuntime(5453):     at com.mypkg.viewP.inflateRow(viewP.java:518)如果只需要设置一个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>使用的时候写成imageView.setTag(R.id.tag_first, "Hello");
imageView.setTag(R.id.tag_second, "Success");就可以了
相关文章
|
7月前
|
XML 关系型数据库 MySQL
【MySQL异常】ExecutorException: Error getting generated key or setting result to parameter object
【MySQL异常】ExecutorException: Error getting generated key or setting result to parameter object
149 0
|
9月前
|
Android开发
Android (int) (Math.random() * 100 % 4) 的结果
Android (int) (Math.random() * 100 % 4) 的结果
34 0
|
11月前
NullPointerException:method 'android.content.BroadcastReceiver.onReceive' on a null object reference
NullPointerException:method 'android.content.BroadcastReceiver.onReceive' on a null object reference
|
JSON Android开发 数据格式
Android:解析Json异常 Expected a string but was BEGIN_OBJECT at
今天解析后端数据时,发现了这个报错:Expected BEGIN_OBJECT but was STRING at 看来是自己哪儿解析错误了。 因为数据的特殊性,后端返回的Json串里面可能还会有Json数据,可能嵌套了三次层,
|
JavaScript
js:Object对象按照key的升序排序
js:Object对象按照key的升序排序
198 0
|
PyTorch TensorFlow 算法框架/工具
TensorflowConv2D:AttributeError: ‘int‘ object has no attribute ‘lower‘
TensorflowConv2D:AttributeError: ‘int‘ object has no attribute ‘lower‘
207 0
TensorflowConv2D:AttributeError: ‘int‘ object has no attribute ‘lower‘
Pandas pd.merge() 报错:ValueError: You are trying to merge on int64 and object columns.
Pandas pd.merge() 报错:ValueError: You are trying to merge on int64 and object columns.
Pandas pd.merge() 报错:ValueError: You are trying to merge on int64 and object columns.
jira项目笔记26-TS object 类型 和 {[key:string]: unknown} 定义类型的区别
jira项目笔记26-TS object 类型 和 {[key:string]: unknown} 定义类型的区别
147 0
|
Java Android开发
Android/Java中解析.crt证书文件的公钥(public key)---Android拓展篇
Android/Java中解析.crt证书文件的公钥(public key)---Android拓展篇
1392 0