android在设计View类时,为了能储存一些辅助信息,设计一个一个setTag/getTag的方法。这让我想起在Winform设计中每个Control同样存在一个Tag。

今天要说的是我最近学习android遇见的setTag的坑。一般情况下我们只需要使用唯一参数的setTag方法。但有时我们需要存储多个数据,所以这个时候我们就需要使用带key的重载。

文档是描述:“ 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.”

这里说明必须保证key的唯一,但是如果我们使用java常量定义key(private static final int TAG_ID = 1;)这样你任然会遇见如下错误:

java.lang.IllegalArgumentException: The key must be an application-specific resource id

正确的解决方案是:

在res/values/strings.xml中定义这个key常量,如下:

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

更多相关文章

  1. Android RSA与Java RSA加密不同标准产生问题的解决方法
  2. Android sdk manager不能更新下载缓慢的解决方法
  3. android截屏代码实现方法
  4. Android自定义控件一简介
  5. Android Cannas.drawTextView 方法坐标问题
  6. android 中的广播 ,系统广播和自定义广播
  7. Android开机自启动程序设置及控制方法
  8. Android随笔之——Android时间、日期相关类和方法

随机推荐

  1. golang 怎么做热更新
  2. GOLANG 为什么还有指针
  3. golang 怎么调用c代码
  4. golang 数组存的是什么
  5. golang 怎么设计一个栈
  6. golang 是面向对象的么
  7. golang 怎么拼接字符串
  8. golang 如何读取csv文件
  9. golang有web框架吗
  10. golang 是否需要orm