在 Android 中,SeekBar 是一種讓使用者可以透過滑動方式選取數值的 UI 元件,通常應用於調整音量、亮度、進度等場景。
SeekBar 與前面提到的 ProgressBar 類似,不同之處在於 SeekBar 具有可由使用者拖曳的滑動控制桿,由於 SeekBar 是繼承自 ProgressBar,因此它也擁有 ProgressBar 的大部分方法,如下。
方法 | 說明 |
---|---|
setProgress(int) |
設定目前進度值 |
getProgress() |
取得目前進度值 |
setMax(int) |
設定最大進度值 |
getMax() |
取得最大進度值 |
讓我們先拉一個SeekBar跟待會用來顯示SeekBar進度值的TextView。
<SeekBar
android:id="@+id/seekBar"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline4"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="目前數值:0/100"
app:layout_constraintBottom_toTopOf="@+id/guideline5"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/guideline4" />
接下來就可以來寫程式了,一樣要記得匯入類別~這很重要!
import android.widget.SeekBar;// 用於進度滑桿
匯入完我們現在來看程式碼,都有寫註解,應該還算好理解。
首先我們先宣告一個Context來存放MainActivity
private Context mContext;
然後儲存當前 Activity 的 Context,因為我們之後會用到Toast
mContext = MainActivity.this;
接下來就可以來設定我們的SeekBar了,透過監聽SeekBar,我們可以分別控制SeekBar被按下、放開跟拖曳時分別要執行甚麼動作
程式碼 | 功能 |
---|---|
onProgressChanged | 當進度改變時觸發 |
onStartTrackingTouch | 當使用者開始按住 SeekBar 時觸發 |
onStopTrackingTouch | 當使用者放開 SeekBar 時觸發 |
//監聽SeekBar
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override // 當進度改變時觸發:progress 為當前值,fromUser 表示是否為使用者操作所致
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textView.setText("目前數值:"+progress+" /100");
}
@Override// 當使用者開始按住 SeekBar 時觸發
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(mContext,"按住Seekbar",Toast.LENGTH_SHORT).show();
}
@Override// 當使用者放開 SeekBar 時觸發
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(mContext,"放開Seekbar",Toast.LENGTH_SHORT).show();
}
});
完整程式碼:
public class MainActivity extends AppCompatActivity {
private SeekBar seekBar; //宣告元件
private TextView textView;
private Context mContext;// 存放MainActivity 的 Context
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;// 儲存當前 Activity 的 Context,供 Toast 使用
bindViews();// 呼叫bindViews
}
private void bindViews(){
seekBar=findViewById(R.id.seekBar);//綁定元件
textView=findViewById(R.id.textView);
//監聽SeekBar
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override // 當進度改變時觸發:progress 為當前值,fromUser 表示是否為使用者操作所致
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textView.setText("目前數值:"+progress+" /100");
}
@Override// 當使用者開始按住 SeekBar 時觸發
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(mContext,"按住Seekbar",Toast.LENGTH_SHORT).show();
}
@Override// 當使用者放開 SeekBar 時觸發
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(mContext,"放開Seekbar",Toast.LENGTH_SHORT).show();
}
});
}
}
執行成果展示: