Progressbar, ratingbar, and spinner controls

Source: Internet
Author: User

1. progressbar, seekbar, and ratingbar controls

The progressbar control, which is our usual progress bar control, can display the loading progress. Seekbar control and slider control, which can be dynamically assigned as needed. In this example, you can drag the seekbar so that the progress bar can display the drag progress in real time. Ratingbar control, Star Bar control, the scoring control we often see, five stars. The following is the code for the Main. XML page. We declare several controls.

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:paddingBottom="@dimen/activity_vertical_margin" 6     android:paddingLeft="@dimen/activity_horizontal_margin" 7     android:paddingRight="@dimen/activity_horizontal_margin" 8     android:paddingTop="@dimen/activity_vertical_margin" 9     tools:context=".MainActivity" >10 11     <TextView12         android:id="@+id/textView1"13         android:layout_width="wrap_content"14         android:layout_height="wrap_content"15         android:text="@string/hello_world" />16 17     <ProgressBar18         android:id="@+id/progressBar1"19         android:layout_width="fill_parent"20         android:layout_height="wrap_content"21         android:layout_alignParentLeft="true"22         android:layout_below="@+id/textView1"23         android:layout_marginTop="68dp"24         android:background="@drawable/color" />25 26     <SeekBar27         android:id="@+id/seekBar1"28         android:layout_width="match_parent"29         android:layout_height="wrap_content"30         android:layout_alignParentLeft="true"31         android:layout_alignParentRight="true"32         android:layout_below="@+id/progressBar1"33         android:layout_marginTop="48dp" />34 35     <RatingBar36         android:id="@+id/ratingBar1"37         android:layout_width="wrap_content"38         android:layout_height="wrap_content"39         android:layout_alignLeft="@+id/seekBar1"40         android:layout_below="@+id/seekBar1"41         android:layout_marginTop="68dp" />42 43     <ProgressBar44         android:id="@+id/progressBar2"45         style="?android:attr/progressBarStyleHorizontal"46         android:layout_width="fill_parent"47         android:layout_height="wrap_content"48         android:layout_alignParentRight="true"49         android:layout_below="@+id/ratingBar1" />50 51 </RelativeLayout>
Control generation

Here we only need to capture the drag events of the seekbar and the events of the ratingbar control. The seekbar event is setonseekbarchangelistener, while the ratingbar event is setonratingbarchangelistener.

1 protected void oncreate (bundle savedinstancestate) {2 super. oncreate (savedinstancestate); 3 setcontentview (R. layout. activity_main); 4 Final seekbar seek = (seekbar) findviewbyid (R. id. seekbar1); 5 Final progressbar process = (progressbar) findviewbyid (R. id. progressbar2); 6 Final ratingbar rating = (ratingbar) findviewbyid (R. id. ratingbar1); 7 rating. setonratingbarchangelistener (New ratingbar. onratingbarchangelistener () {8 9 @ override10 public void onratingchanged (ratingbar, float rating, 11 Boolean fromuser) {12 // todo automatically generated method stub 13 float rate = ratingbar. getrating (); 14 process. setprogress (INT) (rate/5*100); 15} 16}); 17 18 seek. setonseekbarchangelistener (New seekbar. onseekbarchangelistener () {19 20 @ override21 public void onstoptrackingtouch (seekbar) {22 // todo automatically generated method stub 23 process. setprogress (seek. getprogress (); 24} 25 26 @ override27 public void onstarttrackingtouch (seekbar) {28 // todo automatically generated method stub 29 30} 31 32 @ override33 public void onprogresschanged (seekbar, int progress, 34 Boolean fromuser) {35 // todo automatically generated method stub 36 37} 38}); 39}
Event listening

In this way, we have a preliminary understanding of the control of the progress bar.

2. spinner Control

The spinner control and the drop-down list control are similar to the combox control. When using the control, you also need to create an item for it. In string. xml

File to add a new string resource. Bind resources to the spinner control through the adapter in the background.

1 baseadapter basea = new baseadapter () {2 3 @ override 4 Public int getcount () {5 // todo automatically generated method stub 6 RETURN Strid. length; 7} 8 9 @ override10 public object getitem (INT position) {11 // method automatically generated by todo 12 return NULL; 13} 14 15 @ override16 public long getitemid (INT position) {17 // todo automatically generated method stub 18 return 0; 19} 20 21 @ override22 public view getview (INT position, view convertview, 23 viewgroup parent) {24 // The method stub automatically generated by todo 25 linearlayout li = new linearlayout (mainactivity. this); 26 textview text1 = new textview (mainactivity. this); 27 text1.settext ("" + getresources (). gettext (Strid [position]); 28 text1.settextsize (24); 29 Li. addview (text1); 30 return Li; 31 }}; 32 spinner. setadapter (basea );
Bind an adapter

 

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.