天天看點

【Android 開發】:UI控件之拖動條控件 SeekBar的使用方法

SeekBar控件可以通過拖動滑竿改變目前的值,可以利用SeekBar來設定具有一定範圍的變量的值,一般使用者改變螢幕亮度等。

實戰案例一:

SeekBar控件使用。

布局檔案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <SeekBar
        android:id="@+id/seekbar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30" />

    <SeekBar
        android:id="@+id/seekbar2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30"
        android:secondaryProgress="60" />

</LinearLayout>
           

[說明]:

   android:max="100"    android:progress="30"

表示最大刻度為100,進度為30

   android:secondaryProgress="60"

表示第二刻度,一般用在流媒體播放視訊中,第一段表示播放進度,第二段表示下載下傳進度

程式主要代碼:

/*
     * 當滑動滑竿的時候會觸發的事件(non-Javadoc)
     */
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // TODO Auto-generated method stub
        if(seekBar.getId() == R.id.seekbar1){
            textView1.setText("seekbar1的目前位置是: " + progress);
        }else{
            textView2.setText("seekbar2的目前位置是: " + progress);
        }
        
    }

    //表示從哪裡開始拖動
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        if(seekBar.getId() == R.id.seekbar1){
            textView1.setText("seekBar1開始拖動");
        }else {
            textView1.setText("seekBar2開始拖動");
        }
    }

    //表示從哪裡結束拖動
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        if(seekBar.getId() == R.id.seekbar1){
            textView1.setText("seekBar1停止拖動");
        }else {
            textView1.setText("seekBar2停止拖動");
        }
    }
           
程式Demo執行結果:
【Android 開發】:UI控件之拖動條控件 SeekBar的使用方法
【Android 開發】:UI控件之拖動條控件 SeekBar的使用方法