How Android writes an interface for voting or expressing ideas

Source: Internet
Author: User

First:




Put these views in a linearlayout:

<linearlayout        xmlns:android= "http://schemas.android.com/apk/res/android"        android:id= "@+id/repost_ Vote_tag_list "           android:layout_width=" fill_parent "          android:layout_height=" wrap_content "          android:o rientation= "vertical"          android:visibility= "Gone" >      </LinearLayout>

Each item can be implemented like this:

public class Kxtagwidget extends textview{string mtagname = "";/** colors */int mbgcolor = Color.white;int Mpressedbgcolor = Color.white;int Mtextcolor = color.white;int Mpressedtextcolor = color.black;public KXTagWidget (context context) {Supe R (context); setclickable (true);} Public Kxtagwidget (context context, AttributeSet Attrs) {Super (context, attrs); setclickable (true);} Public Kxtagwidget (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Setclickable ( true);} public void SetName (String tagName) {if (tagName! = null) {Mtagname = Tagname;this.settext (Mtagname);}} public void Setbgcolor (int bgColor, int pressedbgcolor) {Mbgcolor = Bgcolor;mpressedbgcolor = Pressedbgcolor; This.setbackgroundcolor (bgColor);//this.set}public int Getmpressedbgcolor () {return mpressedbgcolor;} public void Setmpressedbgcolor (int mpressedbgcolor) {this.mpressedbgcolor = Mpressedbgcolor;} public int Getmpressedtextcolor () {return mpressedtextcolor;} public void Setmpressedtextcolor (int mpreSsedtextcolor) {this.mpressedtextcolor = Mpressedtextcolor;} public int Getmbgcolor () {return mbgcolor;} public void Setmbgcolor (int mbgcolor) {this.mbgcolor = Mbgcolor;} public int Getmtextcolor () {return mtextcolor;} public void Setmtextcolor (int mtextcolor) {this.mtextcolor = Mtextcolor;} public void SetTextColor (int textcolor, int pressedtextcolor) {Mtextcolor = Textcolor;mpressedtextcolor = Pressedtextcolor;} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas);//Draw Border paint paint = new paint ();p aint.setcolor ( this.mbgcolor); int width = getwidth (); int height = getheight (); canvas.drawline (0, 0, width-1, 0, paint); Canvas.drawline ( Width-1, 0, width-1, height, paint); Canvas.drawline (width-1, height-1, 0, height-1, paint); Canvas.drawline (0, he Ight-1, 0, 0, paint);} public int getmesuredwidth () {int mesuredwidth = 0; Paint p = this.getpaint (); Float textWidth = P.measuretext (mtagname); mesuredwidth = (int) TextWidth + This.getcompoundpaddingleft () + This.getcompoundpaddingrIght (); return mesuredwidth;}} 

Implementation of activity:

private static final int vote_bar_image_num = 9;private int mvotebarimage[] = new Int[vote_bar_image_num];p rivate hashmap& Lt  String, view> mvotecontrolmap = new hashmap<string, view> ();p rivate int mtagbgcolor[] = new int[14];/** tag Touch Listener */private Tagontouchlistener mtagontouchlistener = null;/** Tag Click Listener */private tagonclicklistener MTag Onclicklistener = null; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Inittagbgcolor (); Mtaglist.add ("led against Microsoft"); Mtaglist.add ("Mtaglist.add"), Mtaglist.add ("XXX"), Mtaglist.add ("yyyyyy"); Mtaglist.add (" Xxxxxxxxxxxxxx "), Mtaglist.add (" zzzzzzzz "); Mtaglist.add (" Some of these small and medium-sized companies are even often less than national standards even without compensation "); Mtaglist.add (" In accordance with the relevant provisions of the state Labor Law "); Mtaglist.add (" That is, with the Nokia Company "); Mtaglist.add (" state "); Constructviews (); private void Constructviews () {//Total number of votesstring text = "All in one" +string.valueof (11) + "item"; TextView view = (TextView) This.findviewbyid (r.id.repost_voTe_total_num_des); View.settext (text);//friend ' s opinion labeltextview View2 = (TextView) This.findviewbyid ( R.id.repost_vote_friend_opinion_label); View Answerlistview = This.findviewbyid (r.id.repost_vote_anwser_list); View Taglistview = This.findviewbyid (r.id.repost_vote_tag_list); View Inputview = This.findviewbyid (r.id.repost_vote_input_layout); answerlistview.setvisibility (View.GONE); Taglistview.setvisibility (view.visible); inputview.setvisibility (view.visible);//Tag list Viewconstructtaglistview ();//Result list view//Constructresultlistview ();} private void Inittagbgcolor () {int i;for (i = 0; i < 7; ++i) {int r = 231-i * 18;int g = 85-i * 3;int B = 85-i * 3;mtagbgcolor[i] = Color.rgb (R, G, b);} for (i = 7; I < ++i) {int c = 118 + (i-7) * 14;mtagbgcolor[i] = Color.rgb (c, C, c);}} Private arraylist<string> mtaglist = new arraylist<string> ();p rivate void Constructtaglistview () { LinearLayout parent = (linearlayout) Findviewbyid (r.id.repost_vote_tag_list), if (mTagList = = null) {parent.setvisibility (view.gone); return;} if (mtaglist.size () = = 0) {parent.setvisibility (view.gone); return;} Parent.removeallviews (); int screenwidth = Getwindowmanager (). Getdefaultdisplay (). getwidth (); Float totalWidth = Screenwidth-25.0f;int tagtotalnum = Math.min (Mtaglist.size (), 15);/** can put the remaining space of the tag */float surplus = TotalWidth; LinearLayout oneline = Null;oneline = new LinearLayout (this); Layoutparams params = new Layoutparams ();p arams.width = Layoutparams.fill_parent;params.height = Layoutparams.wrap_ Content;params.bottommargin = 1;oneline.setlayoutparams (params); String tagName = ""; for (int i = 0; i < Tagtotalnum; i++) {Kxtagwidget Onetag = (kxtagwidget) getlayoutinflater (). Inflate (R.layout.kx_widget_view, null); tagName = Mtaglist.get (i); Onetag.setname (tagName); Onetag.settag (i); Onetag.setbgcolor (Selecttagbgcolor (i), color.white); o Netag.setonclicklistener (this.mtagonclicklistener); this.mtagonclicklistener = new Tagonclicklistener (); This.mtagontouchlistener = new TaGontouchlistener (); Onetag.setontouchlistener (This.mtagontouchlistener); Onetag.setonclicklistener ( Mtagonclicklistener); float viewwidth = Onetag.getmesuredwidth (); if (Surplus > Viewwidth) {// If the remaining surplus space can accommodate the onetag, then drop the tag on the line and recalculate the remaining space Surplusoneline.addview (Onetag); surplus-= viewwidth;} else {//If you don't fit in a line and add the previous line to it, initialize Surplusparent.addview (oneline); surplus = Totalwidth;oneline = new LinearLayout (this) ; Oneline.setlayoutparams (params); Oneline.addview (Onetag); surplus-= Viewwidth;}} if (null! = Oneline) {Parent.addview (oneline);}} private int Selecttagbgcolor (int index) {if (Index < mtagbgcolor.length) {return mtagbgcolor[index];} else {return Mtag BGCOLOR[MTAGBGCOLOR.LENGTH-1];}} private int selectvotebarimage (int index) {int i = index% Vote_bar_image_num;return mvotebarimage[i];} private void Initvotebarimage () {mvotebarimage[0] = r.drawable.votec1;mvotebarimage[1] = R.DRAWABLE.VOTEC2; MVOTEBARIMAGE[2] = r.drawable.votec3;mvotebarimage[3] = r.drawable.votec4;mvotebarimage[4] = R. drawable.votec5;mvotebarimage[5] = r.drawable.votec6;mvotebarimage[6] = r.drawable.votec7;mvotebarimage[7] = R.DRAWABLE.VOTEC8;MVOTEBARIMAGE[8] = r.drawable.votec9;} Private class Tagontouchlistener implements Ontouchlistener {@Overridepublic Boolean onTouch (View V, motionevent event) { Kxtagwidget Tagview = (kxtagwidget) v;switch (Event.getaction ()) {case Motionevent.action_down: {CharSequence tagName = t Agview.gettext (); Tagview.settextcolor (Tagview.getmpressedtextcolor ()); Tagview.setbackgroundcolor ( Tagview.getmpressedbgcolor ());} Break;case MotionEvent.ACTION_UP:case MotionEvent.ACTION_OUTSIDE:case motionevent.action_cancel: { Tagview.settextcolor (Tagview.getmtextcolor ()); Tagview.setbackgroundcolor (Tagview.getmbgcolor ());} Break;default:break;} return false;}} Private class Tagonclicklistener implements Onclicklistener {@Overridepublic void OnClick (view view) {Integer Tagindex = ( Integer) View.gettag (); String name = Mtaglist.get (Tagindex); Toast.maketext (mainactivity.this, name, Toast.lengtH_long). Show ();}} 


Code downloaded in http://download.csdn.net/detail/baidu_nod/7711097

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.