今天來介紹SeekBar,那我們開始吧.w.
SeekBar 是一個在 Android 開發中相當常見的 UI 元件,它是一個可拖曳的進度條,允許使用者透過滑動來選擇一個範圍內的數值。最常見的應用場景就是調整系統音量或螢幕亮度
屬性 | 說明 | 範例 |
---|---|---|
android:max |
設定SeekBar可選擇的最大數值 | android:max="100" |
android:progress |
設定SeekBar的初始預設數值 | android:progress="20" |
android:secondaryProgress |
設定次要進度的初始值 | android:secondaryProgress="50" |
android:thumb |
設定拖曳鈕的外觀 | android:thumb="@drawable/……" |
為了讓 SeekBar 能夠與應用程式互動,需要設定一個監聽器 (OnSeekBarChangeListener) 來捕捉使用者的操作,下面是它的三個實作:
onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
onStartTrackingTouch(SeekBar seekBar)
onStopTrackingTouch(SeekBar seekBar)
<SeekBar
android:id="@+id/seekBar"
android:layout_width="0dp"
android:layout_height="75dp"
android:max="100"
android:progress="0"
android:secondaryProgress="50"
android:thumb="@drawable/你設定的外觀檔名" />
<TextView
android:id="@+id/value_main_tv"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="目前數值:0/100"
android:textSize="30sp"/>
<TextView
android:id="@+id/secvalue_main_tv"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="次條數值:"
android:textSize="20sp"/>
public class MainActivity extends AppCompatActivity {
private TextView valueTextView, secValueTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SeekBar valueSeekBar = findViewById(R.id.seekbar_main_skb);
valueTextView = findViewById(R.id.value_main_tv);
secValueTextView = findViewById(R.id.secvalue_main_tv);
valueSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@SuppressLint("SetTextI18n")
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
valueTextView.setText("目前數值:" + progress + " /100 ");
secValueTextView.setText("次條數值:" + seekBar.getSecondaryProgress() + " /100 ");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
今天先到這,明天會來介紹用來轉換頁面的Intent,明天見d('∀')