(Transferred from: http://www.cnblogs.com/topcoderliu/archive/2011/06/07/2074419.html)
"Own experience: to Gettag premise is to have Settag"
Settag ()/gettag ()
The Settag (onbect) In view indicates that a particular data is added to the view, which can be taken out later with Gettag ().
You can use multiple buttons to add a listener, and each button sets a different Settag. The listener uses Gettag to tell which button is being pressed.
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
}
}
}
}
Rewrite Baseadapter when writing a ListView
You need to add a button to the button to do the action.
This time, you need the button to set the properties of this line
Button.settag (Item.user.name);
The button event is then processed.
This is useful in some cases where you need to use the adapter custom control display mode.
Adapter has a GetView method, you can use the Settag to cache the lookup view to facilitate multiple reuse
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 ();
}
Other codes can be used directly with Vh.view1, Vh.view2, VH.VIEW3, VH.VIEW4
}
You can look at the source of Android, especially the ListView, you will find this function many times there are magical! hehe!
My understanding is that binding data, especially binding data to view. And can be obtained with Gettag (), very convenient, and is any type of data, really cool.
Useful in GetView (position, convertview, par) of the instance Baseadapter (). Convertview here will use Settag () Gettag (). You can generate Convertview and reuse the widgets inside.
Always feel that this thing is not used Settag (), but as long as you can use it is more skillful
In a program, I've got a lot of buttons. I want to click on one that appears 1 click the second one appears 2
Java code
for (int i = 0; i <; i++) {
Cells[i] = (ImageView) Findviewbyid (Cellids[i]);
Cells[cellnumber++].setonclicklistener (New Onclicklistener () {
public void OnClick (View v) {
Cellclicked (Cellnumber, v);
}
});
}
The code above is inefficient and will appear no matter which one you click 21 of course this is my logic error
How does that make it?
Java code
Onclicklistener listener = new Onclicklistener () {
public void OnClick (View v) {
int cellid = (Integer) v.gettag ();
Cellclicked (Cellid, v);
}
}
View v;
for (int i = 0; i <; i++) {
v = Findviewbyid (cellids[i]);
V.setonclicklistener (listener);
V.settag (i);
}
In this case, an instance is listener, and the value passed by tag is not every instance.
Android Settag ()/gettag ()-(EXT)