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()
原文地址:http://blog.csdn.net/kan1kan5/article/details/39755191