Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 中 setTag() 與 getTag() 的用法,以及為單一 View 設定多組 Tag

編輯:關於 Android 程式設計

要理解 setTag 的用途,先看官方 SDK 的說明:

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 最大的特點,就是把資料綁定到指定控制項上而不顯示出來。setTag 是 View 類別中相當實用的一個方法:與 findViewById 取得物件本身不同,setTag 存進去、getTag 取出來的是物件所指向的資料。

在同一個類別中,用 Tag 存取資料十分方便,不必建資料庫,也比 SharedPreferences 更省記憶體。

TextView tvExecutor = (TextView)findViewById(R.id.t);
tvExecutor.setTag(selectedUserMap);

這裡的 selectedUserMap 可以是 Map<String, String>,也可以是 LinkedList<CarEntity> 這類集合物件,能暫存各種臨時資料。

那麼,一個物件能不能設定多組 Tag 呢?答案是肯定的。

先在 res/values 下的 strings.xml(或 ids.xml)中定義 id:




使用時即可帶入不同的 key:

tvExecutor.setTag(R.id.tag_a, "逗比");
tvExecutor.setTag(R.id.tag_b, "2貨");

convertView 中的 Tag 應用

最常見的用法之一,是在 Adapter 的 getView 裡搭配 ViewHolder 快取:

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);
        convertView.setTag(holder);            // 把 holder 存進 Tag
    } else {
        holder = (ViewHolder) convertView.getTag(); // 取出複用的 holder
    }
    // ……略
    return convertView;
}

在 onClick 事件中透過 Tag 辨識來源

View 的 setTag(Object) 等於替 View 掛上一份外部資料,之後用 getTag() 就能取回。實務上很常用來讓多個 Button 共用同一個監聽器,再靠各自的 Tag 分辨是哪顆按鈕被按下:

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;
            }
        }
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved