How does android write an interface for voting or expressing opinions?

Source: Internet
Author: User

How does android write an interface for voting or expressing opinions?

First:

 

 

Place these views in a LinearLayout:

 

       
 

Each Item can be implemented as follows:

 

 

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) {super (context); setClickable (true);} public KXTagWidget (Context context, AttributeSet attrs) {super (context, attrs ); setClickable (true);} public KXTagWidget (Context con Text, 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); // Add the border Paint paint = new Paint (); paint. 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, height-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]; private HashMap
 
  
MVoteControlMap = new HashMap
  
   
(); Private int mTagBgColor [] = new int [14];/** tag touch listener */private TagOnTouchListener mTagOnTouchListener = null; /** tag click listener */private TagOnClickListener mTagOnClickListener = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initTagBgColor (); mTagList. add (resulting in opposition to Microsoft); mTagList. add (again triggered by sound waves); mTagList. add (dependencies); mTagList. add (XXX); mTagList. add (YYYYYY); mTagList. add (XXXXXXXXXXXXXX); mTagList. add (ZZZZZZZZ); mTagList. add (some small-and medium-sized companies are often below the national standards and do not even pay compensation); mTagList. add (in accordance with the relevant provisions of the national labor law); mTagList. add (in Nokia); mTagList. add (country); constructViews ();} private void constructViews () {// total number of votesString text = A total of + 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 <14; ++ I) {int c = 118 + (I-7) * 14; mTagBgColor [I] = Color. rgb (c, c, c) ;}} private ArrayList
   
    
MTagList = new ArrayList
    
     
(); Private 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 (). getdefadisplay display (). getWidth (); float totalWidth = screenWidth-25366f; int tagTotalNum = Math. Min (mTagList. size (), 15);/** available space for the TAG */float surplus = totalWidth; LinearLayout oneLine = null; oneLine = new LinearLayout (this ); layoutParams params = new LayoutParams (); params. 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); oneTag. setOnClickListener (this. mTagOnClickListener); this. mTagOnClickListener = new TagOnClickListener (); this. mTagOnTouchListener = new TagOnTouchListener (); oneTag. setOnTouchListener (this. mTagOnTouchListener); oneTag. setOnC LickListener (mTagOnClickListener); float viewWidth = oneTag. getMesuredWidth (); if (surplus> viewWidth) {// if the surplus remaining printable space can accommodate oneTag, put the tag in the row and recalculate the remaining printable space surplusoneLine. addView (oneTag); surplus-= viewWidth;} else {// change the line if it cannot accommodate it, and add the previous line to initialize surplusparent. addView (oneLine); surplus = totalWidth; oneLine = new LinearLayout (this); oneLine. setLayoutParams (params); oneLine. addView (oneTag); surplus-= viewWid Th ;}} if (null! = OneLine) {parent. addView (oneLine) ;}} private int selectTagBgColor (int index) {if (index <mTagBgColor. length) {return mTagBgColor [index];} else {return mTagBgColor [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 = tagView. 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 ();}}
    
   
  
 

 

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.