微信扫一扫

028-83195727 , 15928970361
business@forhy.com

Android常用控件之:SeekBar

2016-06-03

/**
 * Android控件之SeekBar介绍 OnSeekBarChangeListener:拖动进度条发生变化监听接口
 * 
 * @description:
 * @author ldm
 * @date 2016-6-3 上午10:39:22
 */
public class SeekBar1 extends Activity implements
        SeekBar.OnSeekBarChangeListener {

    private SeekBar mSeekBar;// 拖动条
    TextView mProgressText;// 进度提示文字
    TextView mTrackingText;// 拖动状态提示文字

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seekbar);
        initViews();
    }

    private void initViews() {
        mSeekBar = (SeekBar) findViewById(R.id.seek);
        mSeekBar.setOnSeekBarChangeListener(this);
        mProgressText = (TextView) findViewById(R.id.progress);
        mTrackingText = (TextView) findViewById(R.id.tracking);
    }

    // 进度长发生改变
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromTouch) {
        mProgressText.setText("当前进度-->" + progress + "是否为用户拖动的滑块-->= "
                + fromTouch);
    }

    // 开始拖动状态监听
    public void onStartTrackingTouch(SeekBar seekBar) {
        mTrackingText.setText("开始拖动");
    }

    // 停止拖动状态监听
    public void onStopTrackingTouch(SeekBar seekBar) {
        mTrackingText.setText("停止拖动");
    }
}

—–布局文件—–

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

    <!-- android:max="100"  进度时大值 -->
    <!-- android:progress="50"   进度条主进度当前值 -->
    <!-- android:secondaryProgress="75" 进度条次进度当前值 -->

    <SeekBar
        android:id="@+id/seek"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="75" />

    <TextView
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tracking"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp" />

</LinearLayout>

 工作中,系统样式的SeekBar通常不能满足用户的眼球,所以我们可以自定义xml文件来实现SeekBar的style风格。