編輯:關於 Android 程式設計
SeekBar(拖動條)與 RatingBar(星級評分條)是 Android 中兩個常見的進度類控件。它們與滾動條外觀相似,但允許使用者透過拖動滑塊來改變進度值。本文將透過實例帶你熟悉這兩個控件。
android:thumb 指定一個 Drawable 物件作為自定義滑塊。

android:isIndicator 是否允許使用者改變(true 為不允許修改)
android:numStars 共有多少個星級
android:rating 預設的星級
android:stepSize 每次變化的星級步長
為了在進度改變時做出相應操作,需要為它們添加監聽:
SeekBar 的監聽:OnSeekBarChangeListener
RatingBar 的監聽:OnRatingBarChangeListener
<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>
以下範例透過 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,我們能即時捕捉使用者的拖動與評分行為,進而驅動界面的動態反饋。這兩個控件在音量調節、亮度設定、評分打星等場景中應用廣泛。
Android 6.0 執行期權限(Runtime Permission)機制源碼剖析
Android 升級到 6.0 之後,為了改善長久以來為人詬病的安全形象,在原本「安裝時一次授予」的基礎上,對其中一部分危險權限改採動態授予的方式,類似各家手機安全助手提
解決Android ScrollView只能容納一個直接子控件的問題
本文將分享Android ScrollView只能添加一個子控件這一常見問題的解決方法,供有需要的朋友參考。 先來看下面這段代碼: <?xml versi
[Android & Java] 設計模式筆記:觀察者模式
觀察者,就像一個人對很多事物都感興趣——音樂、電子產品、遊戲、股票等等,這些事物的任何變化都能引起愛好者的注意並時刻關注。在程式碼中,我們也可以用同樣的思路來設計一些有趣
Android GestureDetector 手勢處理:滑動、刪除與拖曳
GestureDetector 能協助開發者處理點擊、長按、滑動與快速滑動等手勢。使用時通常分為三步:建立 GestureDetector、設定手勢監聽器,並在 onTo