Android Settag ()/gettag () __android

Source: Internet
Author: User
Settag ()/gettag ()

Settag (Onbect) In view adds an extra set of data to the view, which can be taken out later with Gettag ().

You can use multiple button to add a listener, each button setting a different Settag. The listener is 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 listview

Need to add button on no item click button to do the appropriate operation

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 when you need to use a adapter custom control display mode.

Adapter has a GetView method that allows you to use Settag to cache the lookup view for 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 code can be used directly by Vh.view1, Vh.view2, VH.VIEW3, VH.VIEW4

}

You can look at the Android source code, especially have ListView, you will find this function many times have magical functions! 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 the GetView (position, convertview, par) of the instance Baseadapter (). Convertview here will use the Settag () Gettag (). You can generate Convertview and reuse widgets inside.

Always feel that this thing has no use Settag (), but as long as it can be used is more coincidence

In a program I have a lot of button I want to click a 1 click on the second occurrence 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 21 no matter which one you click. Of course it's my logical mistake.

How does that happen?

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, a listener is used, and the value of the tag is not every instance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.