Android---Gettag () and Settag () magical

Source: Internet
Author: User

In Android, Settag (Object) is able to add a special tag to a view and then use Gettag () to get the tag.

用处1:多个button设置不同tag,通过tag区分
new OnClickListener() {  publicvoidonClick(View v) {//获取当前view对应的数字  int cellId = (Integer) v.getTag();  cellClicked(cellId, v);  }  }  View v;  for (int020; i++) {  v = findViewById(cellIDs[i]);  v.setOnClickListener(listener);  v.setTag(i);  }
用法2:在adapter中的getview实现回收的convertView的复用(这代码高亮搞不好,总是出问题)
@Override public View getView (int position, View Convertview, ViewGroup parent) {Phonedemo Demo;if (convertview==null) {Convertview = Layoutinflater. from(context). Inflate(R. Layout. Cell, null);Demo = new Phonedemo ();Demo. Name= (TextView) convertview. Findviewbyid(R. ID. Name);Demo. number= (TextView) convertview. Findviewbyid(R. ID. number);Demo. IMG= (ImageView) convertview. Findviewbyid(R. ID. ImageView1);Demo. Name. SetText(Data. Get(position). GetName());Demo. number. SetText(Data. Get(position). GetNumber());Demo. IMG. Setimageresource(Data. Get(position). GetId());Using tag to achieve recycling resources Convertview. Settag(demo);}else {//here to get the demo that has been recycled, do not re-create, so you can directly reuse, save resources demo = (Phonedemo) convertview. Gettag();Demo. Name. SetText(Data. Get(position). GetName());Demo. number. SetText(Data. Get(position). GetNumber());Demo. IMG. Setimageresource(Data. Get(position). GetId());} return Convertview;} public static class phonedemo{TextView Name,number;ImageView img;}
用处3:当开关来使用,比如对于手电筒,tag设置为true开启,为false,关闭.
//控制语句if ((Booleanfalse) {            startflashlight();        }else{            stopflashlight();        }
protected void Startflashlight() {//Use transitiondrawable to control the animation, that is, flashlight.xml this fileTransitiondrawable drawable = (transitiondrawable) flashlight.getdrawable ();//Control transition time 200 msDrawable.starttransition ( $);//Set identity, if open, then tag is trueFlashlight.settag (true);/ * * Start opening the camera below, then process the image into the background, then set the Flash to open * /        Try{/* * The communication between camera and its components is associated with the parameter parmeters, the camera can get the current state parameter * and then set the new parameter by setparameters */Mcamera = Camera.open ();//Open camera            intTexttrueid =0;I don't know what it is yet.Mcamera.setpreviewtexture (NewSurfacetexture (Texttrueid));            Mcamera.startpreview (); Mparaters = Mcamera.getparameters ();//Set turn on Flash, and lightMparaters.setflashmode (Parameters.flash_mode_torch);        Mcamera.setparameters (mparaters); }Catch(Exception e) {        }    }/ * * Control the closing of the flash * /    protected void Stopflashlight() {transitiondrawable drawable = (transitiondrawable) flashlight.getdrawable ();if((Boolean) Flashlight.gettag ()) {Drawable.reversetransition ( $); Flashlight.settag (false);if(Mcamera! =NULL) {mparaters = Mcamera.getparameters ();                Mparaters.setflashmode (Parameters.flash_mode_off);                Mcamera.setparameters (mparaters); Mcamera.stoppreview ();//Stop previewingMcamera.release ();//Release resources, or you won't be open next timeMcamera =NULL;//set to NULL for garbage collection mechanism recovery as soon as possible}        }    }
上面三个例子都挺常用的,tag的妙用还是能节省不少代码的,对于程序美观性还是有不少作用的.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android---Gettag () and Settag () magical

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.