效果
要点
- 主要方法setMax,setProgress,setSecondaryProgress
- 实现OnSeekBarChangeListener接口,可进行事件监听
- 自定义进度条样式:
android:progressDrawable——进度条
android:thumb——拖动按钮
源码
1、activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:padding="10dip">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv2"/>
</LinearLayout>
2、MainActivity
public class MainActivity extends Activity implements OnSeekBarChangeListener{
private SeekBar seekBar;
private TextView tv1, tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
seekBar.setOnSeekBarChangeListener(this);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
tv1.setText("正在拖动");
tv2.setText("当前进度为"+progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
tv1.setText("开始拖动");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
tv1.setText("停止拖动");
}
}