編輯:關於 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貨");
最常見的用法之一,是在 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;
}
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;
}
}
}
}
像 CSS flexbox 一樣思考:Android 自定義流式布局 FlowLayout 實現
先看需求場景——手淘 HD 的商品詳情頁,「選擇商品屬性」這一塊:問題在於:商品的尺碼(S、M、L、XL、XXL……)數量不固定,而且每個尺碼對應的 View 寬度還不一
Android 內部儲存與外部儲存的檔案讀寫實作
需求: 讓使用者輸入檔名與內容,分別存進手機內部儲存空間與外部儲存(SD 卡),而且兩邊都要能再讀回來。 步驟: 1. 建立名為 CDsaveFile 的 Android
AndroidAnnotations 注入標籤詳解:從 Extra 到 View 綁定
AndroidAnnotations 透過編譯期生成程式碼,把繁瑣的 Intent、View、服務與資源注入變成簡潔標註。本篇文章整理常見 Injection 標籤的使用
Android Studio 使用 Git 完整教程
在 Android Studio 中使用 Git,可以協助開發者進行版本控制、多人協作與專案同步。以下流程從安裝 Git、設定 IDE、建立本地倉庫,到與遠端倉庫互動,說