Android Label Control

Source: Internet
Author: User
Tags security essentials


Version: 1.0Date: 2014.7.24 Copyright: © kince Reprint Annotated source in some applications may need to set some tags to facilitate the use of some information, such as mobile phone assistants or shopping software will have some tags. For the early stage of software development, direct use of textview, button implementation is the simplest way. But this method also has its limitations, such as the inability to control the line-wrapping, low coupling disadvantage.  Therefore, in addition to solving these problems, it is better to encapsulate a class library out, convenient for later use. First create a new tag class,
Import Java.io.serializable;public class Tag implements Serializable {/** * */private static FI           nal long serialversionuid = 2684657309332033242L;      private int backgroundresid;      private int id;      Private Boolean isChecked;      private int leftdrawableresid;      private int rightdrawableresid;      Private String title;           Public tag () {} public tag (int paramint, String paramstring) {this. id = paramint;     this. title = Paramstring;     } public int Getbackgroundresid () {return this. backgroundresid;     } public int getId () {return this. id;     } public int Getleftdrawableresid () {return this. leftdrawableresid;     } public int Getrightdrawableresid () {return this. rightdrawableresid;     } public String GetTitle () {return this. title;   } public boolean isChecked () {return this. isChecked;  } public void Setbackgroundresid (int paramint) {this. backgroundresid = Paramint;     The public void setchecked (Boolean Paramboolean) {this. isChecked = Paramboolean;     } public void SetId (int. paramint) {this. id = paramint;     } public void Setleftdrawableresid (int paramint) {this. leftdrawableresid = Paramint;     } public void Setrightdrawableresid (int paramint) {this. rightdrawableresid = Paramint;     public void Settitle (String paramstring) {this. title = Paramstring; }}
  This class encapsulates the background image resource, ID, check, and so on for the label view.   then new Tagview class, inherited from ToggleButton,
Import Com.niceapp.lib.tagview.r;import Android.content.context;import Android.util.attributeset;import      Android.widget.togglebutton;public class Tagview extends ToggleButton {private Boolean mcheckenable = true;          Public Tagview (Context paramcontext) {super (Paramcontext);     Init (); } public Tagview (Context paramcontext, AttributeSet paramattributeset) {super (Paramcontext, paramattribut          ESet);     Init (); } public Tagview (Context paramcontext, attributeset paramattributeset, int paramint) {Super          (Paramcontext, Paramattributeset, 0);     Init ();          } private void Init () {Settexton (null);          Settextoff (NULL);          SetText ("");     Setbackgroundresource (r.drawable. TAG_BG);           The public void Setcheckenable (Boolean Paramboolean) {this. mcheckenable = Paramboolean; if (!this. mcheckenable) {super. Setchecked (FALSE);          }} public void Setchecked (Boolean Paramboolean) {if (this. mcheckenable) {          Super. Setchecked (Paramboolean); }     }}
This tagview is the tag view, and the tag information is displayed by him. The corresponding XML file is as follows, Tag.xml:
<? XML version= "1.0" encoding = "Utf-8"?>< com.niceapp.lib.tagview.widget.TagView xmlns:android = "http// Schemas.android.com/apk/res/android "    android:layout_width=" wrap_content "    android:layout_height=" Wrap_ Content "    android:drawablepadding=" 5.0dip "    android:minheight=" 0.0dip "    android:paddingbottom=" 4.5dip "    android:paddingleft=" 20.0dip "    android:paddingright=" 20.0dip "    android:paddingtop=" 4.5dip    " Android:textcolor= "#ff000000"    android:textsize= "16.0sp"/>
shown below:On GitHub there's aAndroid-flowlayoutcontrol, which dynamically wraps the view according to the size of the child view,
 As a result, control of line breaks can be implemented using this control without having to reinvent the wheel. The class implemented by the Android-flowlayout feature is FlowLayout, so the implementation of the label control is accomplished by inheriting the class.
Import Java.util.arraylist;import java.util.list;import com.niceapp.lib.tagview.r;import android.content.Context; Import Android.util.attributeset;import Android.util.typedvalue;import Android.view.view;import Android.view.view.onclicklistener;import android.widget.compoundbutton;/*** @author kince* */public class     Taglistview extends FlowLayout implements Onclicklistener {private Boolean misdeletemode;     Private Ontagcheckedchangedlistener Montagcheckedchangedlistener;     Private Ontagclicklistener Montagclicklistener;     private int mtagviewbackgroundresid;     private int mtagviewtextcolorresid;     Private final list<tag> mtags = new arraylist<tag> ();          /** * @param context */public Taglistview (context context) {super (context);     TODO auto-generated Constructor stub init (); }/** * @param context * @param attributeset */Public Taglistview (context context, AttributeSet Attri Buteset) {suPer (context, attributeset);     TODO auto-generated Constructor stub init (); }/** * @param context * @param attributeset * @param defstyle */public Taglistview (Context Conte          XT, AttributeSet attributeset, int defstyle) {Super (context, AttributeSet, Defstyle);     TODO auto-generated Constructor stub init (); } @Override public void OnClick (View v) {if ((v instanceof Tagview)) {tag Localtag = (tag               ) V.gettag ();               if (This.montagclicklistener! = null) {This.mOnTagClickListener.onTagClick ((Tagview) v, Localtag);          }}} private void Init () {} private void Inflatetagview (Final Tag T, Boolean b) {          Tagview Localtagview = (tagview) view.inflate (GetContext (), r.layout.tag, NULL);          Localtagview.settext (T.gettitle ());          Localtagview.settag (t); if (mtagviewtextcolorresid <= 0) {int c = getresources (). GetColor (R.color.blue);          Localtagview.settextcolor (c);               } if (mtagviewbackgroundresid <= 0) {mtagviewbackgroundresid = R.DRAWABLE.TAG_BG;          Localtagview.setbackgroundresource (MTAGVIEWBACKGROUNDRESID);          } localtagview.setchecked (T.ischecked ());          Localtagview.setcheckenable (b); if (misdeletemode) {int k = (int) typedvalue.applydimension (1, 5.0F, GetContext ().               Getresources (). Getdisplaymetrics ());                         Localtagview.setpadding (Localtagview.getpaddingleft (), Localtagview.getpaddingtop (), K,               Localtagview.getpaddingbottom ());          Localtagview.setcompounddrawableswithintrinsicbounds (0, 0, r.drawable.forum_tag_close, 0); } if (T.getbackgroundresid () > 0) {localtagview.setbackgroundresource (T.Getbackgroundresid ()); } if ((T.getleftdrawableresid () > 0) | | (T.getrightdrawableresid () > 0))  {Localtagview.setcompounddrawableswithintrinsicbounds (T.getleftdrawableresid (), 0,          T.getrightdrawableresid (), 0);          } localtagview.setonclicklistener (this);                         Localtagview. Setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {                                   public void OnCheckedChanged (Compoundbutton Paramanonymouscompoundbutton, Boolean Paramanonymousboolean) {t.setchecked (Paramanonymousboo                              Lean); if (TagListView.this.mOnTagCheckedChangedListener! = null) {TAGLISTVIEW.THIS.MONTAGCHEC                                                  Kedchangedlistener. Ontagcheckedchanged (     (Tagview) Paramanonymouscompoundbutton, T);          }                         }                    });     AddView (Localtagview);     } public void Addtag (int i, String s) {Addtag (I, S, false);     } public void Addtag (int i, String s, Boolean b) {Addtag (new Tag (I, s), b);     } public void Addtag (tag tag) {Addtag (tag, false);          } public void Addtag (tag tag, Boolean b) {Mtags.add (tag);     Inflatetagview (tag, b);     } public void Addtags (list<tag> lists) {Addtags (lists, false);               The public void Addtags (List<tag> lists, Boolean B) {for (int i = 0; i < lists.size (); i++) {          Addtag ((TAG) lists.get (i), b);     }} public list<tag> GetTags () {return mtags;     Public View Getviewbytag (tag tag) {return Findviewwithtag (tag); } public void Removetag (Tag tag) {Mtags.remove (tag);     Removeview (Getviewbytag (tag));     public void Setdeletemode (Boolean b) {misdeletemode = b;           } public void Setontagcheckedchangedlistener (Ontagcheckedchangedlistener ontagcheckedchangedlistener) {     Montagcheckedchangedlistener = Ontagcheckedchangedlistener; } public void Setontagclicklistener (Ontagclicklistener ontagclicklistener) {Montagclicklistener = OnTagClick     Listener;     } public void Settagviewbackgroundres (int res) {mtagviewbackgroundresid = res;     } public void Settagviewtextcolorres (int res) {mtagviewtextcolorresid = res;     } public void Settags (list<? extends tag> lists) {Settags (lists, false);          } public void Settags (list<? extends Tag> lists, Boolean b) {removeallviews ();          Mtags.clear (); for (int i = 0; i < lists.size (); i++) {Addtag (TAG) lists. get (i), b); }} public static abstract interface Ontagcheckedchangedlistener {public abstract void Ontagcheckedchang     Ed (Tagview Tagview, tag tag); } public static Abstract interface Ontagclicklistener {public abstract void Ontagclick (Tagview tagview, Tag     TAG); }}
  The most desired part of this class is the Inflatetagview method, which parses the Tagview out and then displays the label Taglistview to be displayed. The code for the   last activity is as follows:
Import Java.util.arraylist;import Java.util.list;import Com.niceapp.lib.tagview.widget.tag;import Com.niceapp.lib.tagview.widget.taglistview;import Android.app.activity;import Android.os.Bundle;public Class     Mainactivity extends Activity {private Taglistview mtaglistview;     Private final list<tag> mtags = new arraylist<tag> (); Private final string[] titles = {"Security essentials", "Music", "Parents Learn", "office worker Essentials", " Mobile Defender", "QQ", "Input Method", "", "most Beautiful Application", "Andevui     "," Mushroom Street "};          @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);          Setcontentview (r.layout.select_tag_activity);          Mtaglistview = (Taglistview) Findviewbyid (R.id.tagview);          SetupData ();     Mtaglistview.settags (Mtags);               private void SetupData () {for (int i = 0; i <; i++) {tag tag = new Tag ();               Tag.setid (i);               Tag.setchecked (TRUE); Tag.settitle (Titles[i]);              Mtags.add (tag); }     }}
The real-computer display works as follows:Of course, the appearance of this tagview can be set on its own, including fonts, backgrounds and so on. Project code: http://download.csdn.net/detail/wangjinyu501/7673827





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.