Seekbar 是可拖动的进度条,用于用户实现视频和音乐快进、倒退的功能。
主要方法:
- setMax---设置SeekBar的最大数值
- setProgress---设置seekBar当前的数值
- setSecondaryProgress---设置SeekBar的第二数值,即当前拖动条推荐的数值。
SeekBar的监听事件:
通过实现OnSeekBarChangeListener接口,需要实现3个方法:
- 数值改变(onProgressChange)
- 开始拖动(onStartTrackingTouch)
- 停止拖动(onStopTrackingTouch)
自定义SeekBar进度条:
(改变进度条样式)
android: progressDrawable=“@drawable/seekbar_img”
(改变滑块的样式)
android: thumb=“@drawable/thumb”
example:
------------------------------------------------------------------------------------------------------------
public class MainActivity extends Activity implements OnSeekBarChangeListener{
private SeekBar seekbar;
private TextView tv1;
private TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekbar = (SeekBar) findViewById(R.id.seekBar);
seekbar.setOnSeekBarChangeListener(this);
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
}
//数值改变
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
tv2.setText("当前数值为:"+progress);
tv1.setText("正在拖动");
}
//开始拖动
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
tv1.setText("开始拖动");
}
//停止拖动
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
tv1.setText("停止拖动");
}
}
---------------------------------------------------------------------------------------------------------------
//my_thumb.xml文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=" http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/one" android:state_pressed="true"></item>
<item android:drawable="@drawable/one" android:state_focused="true"></item>
<item android:drawable="@drawable/one" android:state_selected="true"></item>
<item android:drawable="@drawable/two"></item>
</selector>