Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 學習筆記:SeekBar 與 RatingBar 的使用

編輯:關於 Android 程式設計

SeekBar(拖動條)與 RatingBar(星級評分條)是 Android 中兩個常見的進度類控件。它們與滾動條外觀相似,但允許使用者透過拖動滑塊來改變進度值。本文將透過實例帶你熟悉這兩個控件。

一、SeekBar 特有的 XML 屬性

android:thumb 指定一個 Drawable 物件作為自定義滑塊。

二、RatingBar 特有的 XML 屬性

android:isIndicator 是否允許使用者改變(true 為不允許修改)
android:numStars 共有多少個星級
android:rating 預設的星級
android:stepSize 每次變化的星級步長

三、監聽器

為了在進度改變時做出相應操作,需要為它們添加監聽:

SeekBar 的監聽:OnSeekBarChangeListener
RatingBar 的監聽:OnRatingBarChangeListener

四、布局文件 activity_main.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView android:id="@+id/imgView1"
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher"/>
    <SeekBar android:id="@+id/seekBar"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:thumb="@drawable/ic_launcher"
        android:max="255"
        android:progress="255"/>
    <ImageView android:id="@+id/imgView2"
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:src="@drawable/img01"/>
    <RatingBar android:id="@+id/reatingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="255"
        android:rating="5"
        android:stepSize="0.5"
        android:numStars="5"/>
</LinearLayout>

五、MainActivity.java

以下範例透過 SeekBar 與 RatingBar 的進度變化,即時改變圖片的透明度:

public class MainActivity extends Activity {
    private ImageView imgViewSB = null;
    private ImageView imgViewRB = null;
    private SeekBar seekBar = null;
    private RatingBar ratingBar = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.imgViewSB = (ImageView) findViewById(R.id.imgView1);
        this.imgViewRB = (ImageView) findViewById(R.id.imgView2);
        this.seekBar = (SeekBar) findViewById(R.id.seekBar);
        this.ratingBar = (RatingBar) findViewById(R.id.reatingBar);

        // seekBar 設置監聽,改變圖片透明度
        this.seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.i("onStopTrackingTouch", "停止拖動觸發的方法");
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Log.i("onStartTrackingTouch", "開始拖動觸發的方法");
            }
            /** progress:拖動條的進度;fromUser:是否使用者手動改變 */
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                imgViewSB.setAlpha(progress); // 設置圖片的透明度
                Log.i("onProgressChanged", "拖動滑塊位置發生改變時觸發的方法");
            }
        });

        // ratingBar 設置監聽,改變圖片透明度
        this.ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
            /** ratingBar:RatingBar 物件;rating:星級大小;fromUser:是否使用者手動改變 */
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                imgViewRB.setAlpha((int) (rating * 255 / 5));
            }
        });
    }
}

運行後的效果如下:

透過 SeekBar 的 onProgressChanged 與 RatingBar 的 onRatingChanged,我們能即時捕捉使用者的拖動與評分行為,進而驅動界面的動態反饋。這兩個控件在音量調節、亮度設定、評分打星等場景中應用廣泛。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved