首先我们要知道setTag方法是干什么的,SDK解释为
Tags
Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.
Tag不像ID是用标示view的。Tag从本质上来讲是就是相关联的view的额外的信息。
它们经常用来存储一些view的数据,这样做非常方便而不用存入另外的单独结构。
Tag有一个特点,绑定数据到指定控件(view),而不显示出来。
setTag是android的view类中很有用的一个方法,
与setId()不同,findViewById找到的是对象本身。
而setTag(),取出来的是对象所指向的对象。
在同一个类中,存取数据很方便,不用数据库建库,
也比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>
其实用的最多的是convertView中
1. 对于使用了LayoutInflater对象进行View扩充的Tag的使用
在之前,在adapter中,我们在getView中是这么些的代码:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.vlist2, null);
holder.img = (ImageView) convertView.findViewById(R.id.img);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.info = (TextView)
convertView.findViewById(R.id.info);
// setTag的妙用
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
……略
}
例子3 在onClick点击事件中,使用tag
View中的setTag(Onbect)表示给View添加一个格外的数据,以后可以用getTag()将这个数据取出来。
可以用在多个Button添加一个监听器,每个Button都设置不同的setTag。这个监听器就通过getTag来分辨是哪个Button 被按下。
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.Button01);
Button button2 = (Button) findViewById(R.id.Button02);
Button button3 = (Button) findViewById(R.id.Button03);
Button button4 = (Button) findViewById(R.id.Button04);
MyListener listener = new MyListener();
button1.setTag(1);
button1.setOnClickListener(listener);
button2.setTag(2);
button2.setOnClickListener(listener);
button3.setTag(3);
button3.setOnClickListener(listener);
button4.setTag(4);
button4.setOnClickListener(listener);
}
public class MyListener implements View.OnClickListener {
@Override
public void onClick(View v) {
int tag = (Integer) v.getTag();
switch (tag) {
case 1:
System.out.println("button1 click");
break;
case 2:
System.out.println("button2 click");
break;
case 3:
System.out.println("button3 click");
break;
case 4:
System.out.println("button4 click");
break;
}
}
}
}