天天看点

【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的使用方法