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 classMain extends Activity {@Override Public voidonCreate (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=NewMyListener (); 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 classMyListener implements View.onclicklistener {@Override Public voidOnClick (View v) {intTag =(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
PublicView GetView (intposition, 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=NewViewholder (); 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 (int0; i++= (ImageView) Findviewbyid (Cellids[i]); Cells[cellnumber++].setonclicklistener (new Onclicklistener () { publicVoid OnClick (View v) {cellclicked (Cellnumber, v); } }); }
The above code is inefficient and will appear no matter which one you click 21 of course this is my logic error, How to achieve it?
Java code:
New Onclicklistener () { publicvoid OnClick (View v) { int cellid = (I Nteger) V.gettag (); Cellclicked (Cellid, v); }} View v; for (int0; i++= Findviewbyid (cellids[i]); V.setonclicklistener (listener); V.settag (i); }
Android Settag ()/gettag ()