Android setTag()與getTag(),與set多個setTag()
首先我們要知道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中
convertView中的TAG
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點擊事件中。使用tagsetTag()/getTag()
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 {
[email protected]
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 {
[email protected]
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;
}
}
}
}
Android setTag()與getTag(),與set多個setTag()