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

Android 的 SeekBar 與 RatingBar 使用詳解

編輯:關於 Android 程式設計

SeekBar 簡介

SeekBar 允許用戶拖動以進行調節,常用於音量、亮度等場景。通過 android:thumb 可以設置表示拖動滑塊的 drawable;通過 setOnSeekBarChangeListener() 可以監聽 SeekBar 的變化。

代碼示例

下面用 SeekBar 來控制一張圖片的透明度,滑塊取值 0~255 對應 alpha:

public class MainActivity extends Activity {
    private ImageView image;
    private SeekBar seekBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.image);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                image.setAlpha(progress);
            }
            @Override public void onStartTrackingTouch(SeekBar seekBar) { }
            @Override public void onStopTrackingTouch(SeekBar seekBar) { }
        });
    }
}

布局代碼

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="240px"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="255" />
</LinearLayout>

效果圖

RatingBar 簡介

RatingBar 是一個星級評分條,它與 SeekBar 擁有共同的父類 AbsSeekBar,因此許多屬性頗為相似。

代碼示例

用 RatingBar 打分來控制圖片透明度(滿分 5 星,映射到 0~255):

public class MainActivity extends Activity {
    private ImageView image;
    private RatingBar ratingBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.image);
        ratingBar = (RatingBar) findViewById(R.id.ratingBar);
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                image.setAlpha(rating * 255 / 5);
            }
        });
    }
}
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="240px"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="0.5" />
</LinearLayout>

效果圖

可以看到,無論是 SeekBar 還是 RatingBar,都能即時響應用戶操作並驅動界面變化。其中 RatingBar 的 numStars 決定星級數量,stepSize 決定每次打分的步長,開發時可按需調整。

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