首页 > 代码库 > Android setTag()与getTag(),与set多个setTag()
Android setTag()与getTag(),与set多个setTag()
setTag是android的view类中很有用的一个方法,
在同一个类中,存取数据很方便,不用数据库建库,
也比sharePreference更节约内存。
setTag(Object tag)方法比较简单
比如
TextView tvExecutor = (TextView)findViewById(R.id.t);
tvExecutor.setTag(selectedUserMap);
这个selectedUserMap
可以使Map<String, String> selectedUserMap这个对象。
也可以是LinkedList<CarEntity> mList 这个对象。
可以存储各种临时数据,但是还不能称其为存储方式。
在同一个类中,我们可以使用setTag(),getTag(),存取数据。
可不可以一个对象,设置多个Tag呢?答案是肯定的
在res/values/strings.xml中添加
<span face="Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span style="line-height: 18px;"><span style="color:#cc0000;"><resources></span></span></span>
<span style="color:#cc0000;"><span face="Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><item type="id" name="tag_a</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; background-color: rgb(240, 240, 240);">"></item></span></span>
<span face="Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span style="color:#cc0000;"><item type="id" name="tag_b"></item></span></span>
<span face="Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span style="color:#cc0000;"></resources></span></span>
<span face="Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"></span>
在activity中,使用的时候写成
<span face="Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"></span>
<code style="border: 0px; vertical-align: baseline; background-color: rgb(238, 238, 238); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; padding: 0px; margin: 0px;"><span style="color:#cc0000;"><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;"><span style="font-family: Arial; font-size: 14px; line-height: 26px;">tvExecutor</span></span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">.</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">setTag</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">(</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">R</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">.</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;"><span color="#00008b">id.tag_a</span></span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">,</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;"> </span><span class="str" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">"逗比"</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">);</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;"> <span style="font-family: Arial; font-size: 14px; line-height: 26px;">tvExecutor</span></span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">.</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">setTag</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">(</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">R</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">.</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;"><span color="#00008b">id.tag_b</span></span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">,</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;"> </span><span class="str" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">"2货"</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">);</span></span></code>
Android setTag()与getTag(),与set多个setTag()
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。