Android: Use the setTag () and getTag () Methods of View

Source: Internet
Author: User

Android: Use the setTag () and getTag () Methods of View

We usually use the findViewById () method to obtain the View control we want to use. However, in addition to this method, we can also use the setTag (Onbect) in the View) add an extra data to the View and use getTag () to obtain the corresponding View. The setTag () and getTag () methods are often used to process multiple identical views. For example, if we want to add click events for several buttons, we can use View. getId () is used to differentiate buttons. You can also set tags for buttons to differentiate them. For example:

Public class TagTestActivity extends Activity implements OnClickListener {
Private Button tagBtn2, tagBtn3, tagBtn4;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
InitViews ();
DealBtnsListeners ();
}
Private void initViews (){
TagBtn2 = (Button) findViewById (R. id. tag_btn_2 );
TagBtn3 = (Button) findViewById (R. id. tag_btn_3 );
TagBtn4 = (Button) findViewById (R. id. tag_btn_4 );
}
Private void dealBtnsListeners (){
TagBtn2.setOnClickListener (this );
TagBtn3.setOnClickListener (this );
TagBtn4.setOnClickListener (this );
TagBtn2.setTag (2 );
TagBtn3.setTag (3 );
TagBtn4.setTag (4 );
}
@ Override
Public void onClick (View v ){
// We usually determine which Button is clicked Based on the Control id. Here we use getTag () to determine
Int btnId = (int) v. getTag ();
Switch (btnId ){
Case 2:
Toast. makeText (this, "click the Button with Tag 2", 3000). show ();
Break;
Case 3:
Toast. makeText (this, "click the Button whose Tag is 3", 3000). show ();
Break;
Case 4:
Toast. makeText (this, "click the Button with Tag 4", 3000). show ();
Break;
Default:
Break;
}
}
}

However, the most common use of tags is not described above. Those who have used ListView are certainly familiar with tags. To improve program performance, we need to use tags when writing Adapter adapters for ListView. The following code must have been used:

Static class ViewHolder {
Private TextView tagTv01;
Private TextView tagTv02;
}

Public View getView (int position, View convertView, ViewGroup parent ){
If (convertView = null ){
LayoutInflater inflater = mContext. getLayoutInflater ();
ConvertView = inflater. inflate (R. layout. item_tag, null );
ViewHolder h_= new ViewHolder ();
Vl. tagTv01 = (TextView) convertView. findViewById (R. id. tagTv01 );
Vl. tagTv02 = (TextView) convertView. findViewById (R. id. tagTv01 );
ConvertView. setTag (Flac );
} Else {

Vl = (ViewHolder) convertView. getTag ();

}
// Add data ........
}

In addition to the above two cases, we generally do not recommend using setTag () and getTag (), mainly considering the readability of the Code. In addition, the Object is transmitted in setTag (Objcet, there is an object conversion problem.

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.