原文地址:http://www.cnblogs.com/qingblog/archive/2012/07/03/2575140.html
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;
- }
- }
- }
- }
[java] view plaincopy
- 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;
- }
- }
- }
- }
在写listView 时候要重写BaseAdapter
需要在每个item都加上button 点击button做相应操作
这个时候需要button里面设置下 这行的属性button.setTag(item.user.name);
然后处理button事件。
这个东西在一些需要用到Adapter自定控件显示方式的时候非常有用
Adapter 有个getView方法,可以使用setTag把查找的view缓存起来方便多次重用
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder vh;
- if (convertView == null){
- LayoutInflater inflater = (LayoutInflater) mWidgetsSwitchApp .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- convertView = inflater.inflate(R.layout.app_gallery_item, null);
- vh = new ViewHolder();
- vh.view1 = (ImageView) convertView.findViewById(R.id.view1);
- vh.view2 = (ImageView) convertView.findViewById(R.id.view2);
- vh.view3= (ImageView) convertView.findViewById(R.id.view3);
- vh.view4 = (ImageView) convertView.findViewById(R.id.view4);
- convertView.setTag(vh);
- }else{
- vh = (ViewHolder) convertView.getTag();
- }
- //其他的代码可以直接使用 vh.view1、vh.view2、vh.view3 、vh.view4
- }
[java] view plaincopy
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder vh;
- if (convertView == null){
- LayoutInflater inflater = (LayoutInflater) mWidgetsSwitchApp .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- convertView = inflater.inflate(R.layout.app_gallery_item, null);
- vh = new ViewHolder();
- vh.view1 = (ImageView) convertView.findViewById(R.id.view1);
- vh.view2 = (ImageView) convertView.findViewById(R.id.view2);
- vh.view3= (ImageView) convertView.findViewById(R.id.view3);
- vh.view4 = (ImageView) convertView.findViewById(R.id.view4);
- convertView.setTag(vh);
- }else{
- vh = (ViewHolder) convertView.getTag();
- }
- //其他的代码可以直接使用 vh.view1、vh.view2、vh.view3 、vh.view4
- }
你可以看看android的源码,特别有listview的,你就会发现这个函数很多时候有妙用!呵呵!
我的一点理解是,绑定数据,特别是绑定数据到view。而且可以用getTag()取得,很方便,而且是任意类型的数据,真的很酷。
在实例BaseAdapter()的getView(position, convertView, par)里有用到。convertView这里会用到setTag() getTag()。就可以生成convertView并复用里面的widget
一直觉得这个东西没啥用setTag(),不过只要能用上 就是比较巧的了
在一个程序中呢 我有好多个button 我想点击一个出现1 点击第二个出现2
- for (int i = 0; i < 20; i++) {
- cells[i] = (ImageView) findViewById(cellIDs[i]);
- cells[cellnumber++].setOnClickListener(new OnClickListener() {
- public void onClick(View v){
- cellClicked(cellnumber, v);
- }
- });
- }
[java] view plaincopy
- for (int i = 0; i < 20; i++) {
- cells[i] = (ImageView) findViewById(cellIDs[i]);
- cells[cellnumber++].setOnClickListener(new OnClickListener() {
- public void onClick(View v){
- cellClicked(cellnumber, v);
- }
- });
- }
上面的代码效率不高 而且 无论点击哪一个都会出现21 当然这是我逻辑错误那怎么实现呢
- OnClickListener listener = new OnClickListener() {
- public void onClick(View v){
- int cellId = (Integer) v.getTag(); cellClicked(cellId, v);
- }
- }
- View v;
- for (int i = 0; i < 20; i++){
- v = findViewById(cellIDs[i]);
- v.setOnClickListener(listener);
- v.setTag(i);
- }
[java] view plaincopy
- OnClickListener listener = new OnClickListener() {
- public void onClick(View v){
- int cellId = (Integer) v.getTag(); cellClicked(cellId, v);
- }
- }
- View v;
- for (int i = 0; i < 20; i++){
- v = findViewById(cellIDs[i]);
- v.setOnClickListener(listener);
- v.setTag(i);
- }
这样呢就实例话一个listener,同时通过tag传值就不是每一个实例都做了