控件_SeekBar与RatingBar

这两种进度条都是ProgressBar的子类

SeekBar:是一种可以拖动的进度条,比如播放音乐的进度

 1 import android.app.Activity;

 2 import android.os.Bundle;

 3 import android.widget.SeekBar;

 4 import android.widget.SeekBar.OnSeekBarChangeListener;

 5 

 6 public class MainActivity extends Activity {

 7     private SeekBar seekbar;

 8     protected void onCreate(Bundle savedInstanceState) {

 9         super.onCreate(savedInstanceState);

10         setContentView(R.layout.activity_main);

11         

12         seekbar = (SeekBar) findViewById(R.id.seekbar);

13         seekbar.setProgress(30);

14         seekbar.setSecondaryProgress(50);

15         seekbar.setOnSeekBarChangeListener(new SeekBarListener());

16     }

17     

18     class SeekBarListener implements OnSeekBarChangeListener{

19         /*

20          * seekbar 该对象指的是触发了监听器的SeekBar对象,就是说哪个SeekBar触发了监听器(因为有可能多个SeekBar绑定到这个监听器上)

21          * progress 指的是当前SeekBar的进度

22          * fromUser 表示进度的改变是否来自于用户,是则为true

23          * */

24         //该方法当进度条变化的时候调用

25         public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {

26             System.out.println("progress="+progress+",fromUser="+fromUser);

27         }

28         

29         //当用户滑动进度条的时候调用

30         public void onStartTrackingTouch(SeekBar seekBar) {

31             System.out.println("Onstart");

32         }

33 

34         //当用户终止滑动的时候调用

35         public void onStopTrackingTouch(SeekBar seekBar) {

36             System.out.println("Onstop");

37         }

38         

39     }

40 }

 

 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     <SeekBar

12         android:id="@+id/seekbar"

13         android:layout_width="match_parent"

14         android:layout_height="wrap_content"

15         android:max="100"

16         />

17 

18 </RelativeLayout>

 

 

RatingBar:星星,就是用于评价星级

 

 1 import android.app.Activity;

 2 import android.os.Bundle;

 3 import android.view.View;

 4 import android.view.View.OnClickListener;

 5 import android.widget.Button;

 6 import android.widget.RatingBar;

 7 import android.widget.RatingBar.OnRatingBarChangeListener;

 8 

 9 public class MainActivity extends Activity {

10     private RatingBar ratingbar;

11     private Button button;

12     protected void onCreate(Bundle savedInstanceState) {

13         super.onCreate(savedInstanceState);

14         setContentView(R.layout.activity_main);

15         

16         ratingbar = (RatingBar) findViewById(R.id.ratingbar);

17         ratingbar.setOnRatingBarChangeListener(new RatingBarListener());

18         button = (Button)findViewById(R.id.button);

19         button.setOnClickListener(new ButtonListener());

20         

21         

22     }

23     

24     //该监听器只有onRatingChanged一个方法,第一个参数表示那个对象触发了监听器,第二个参数表示当前星数,第三个参数表示是否来自用户的改变

25     class RatingBarListener implements OnRatingBarChangeListener{

26         public void onRatingChanged(RatingBar ratingbar, float rating, boolean fromUser) {

27             System.out.println("rating="+rating+",fromUser="+fromUser);

28         }

29     }

30     

31     class ButtonListener implements OnClickListener{

32         public void onClick(View v) {

33             ratingbar.setRating(ratingbar.getRating()+0.5f);

34         }

35     }

36 }

 

 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     <RatingBar

12         android:id="@+id/ratingbar"

13         android:layout_width="wrap_content"

14         android:layout_height="wrap_content"

15         android:numStars="5"

16         android:stepSize="0.5"

17         />

18     

19     <Button 

20         android:id="@+id/button"

21         android:layout_width="wrap_content"

22         android:layout_height="wrap_content"

23         android:layout_below="@id/ratingbar"

24         android:text="button"

25         />

26 

27 </RelativeLayout>

28 <!-- 

29   android:numStars="4"     表示设置多少个星通常都是5个星

30   android:progress="3"     设置当前的星数

31   android:stepSize="0.5"   滑动的时候表示每次请进的距离,0。5就是每次半颗星,1就是一个星

32  -->

 

你可能感兴趣的:(RatingBar)