(一)概述
SeekBar ,我们先来看看SeekBar的类结构:
(二)SeekBar的基本用法
运行结果:
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="com.example.adnroid_seekbar.MainActivity" >
<TextView
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<SeekBar android:id="@+id/seekbar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30"/>
<SeekBar android:id="@+id/seekbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30"
android:secondaryProgress="60"/>
</LinearLayout>
java代码:
public class MainActivity extends Activity implements OnSeekBarChangeListener{
private TextView textView1 , textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) this.findViewById(R.id.textview1);
textView2 = (TextView) this.findViewById(R.id.textview2);
SeekBar seekBar1 = (SeekBar) this.findViewById(R.id.seekbar1);
SeekBar seekBar2 = (SeekBar) this.findViewById(R.id.seekbar2);
seekBar1.setOnSeekBarChangeListener(this);
seekBar2.setOnSeekBarChangeListener(this);
}
// 当滑动滑竿的时候会触发的事件
@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{
textView2.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停止拖动");
}
}
}
(三)简单的自定义SeekBar
运行效果:
通过滑动状态来控制Drawable的xml文件:(在drawable目录下新建xml文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/seekbar_thumb_pressed"/>
<item android:state_pressed="false" android:drawable="@drawable/seekbar_thumb_normal"/>
</selector>
控制进度条的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<shape>
<solid android:color="#ABCDFF"/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip >
<shape>
<solid android:color="#DDFF67"/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip >
<shape>
<solid android:color="#BBFF89"/>
</shape>
</clip>
</item>
</layer-list>
然后,在主xml文件中设置如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="158dp"
android:maxHeight="5.0dp"
android:maxWidth="5.0dp"
android:progressDrawable="@drawable/sb_bar"
android:thumb="@drawable/sb_thumb"/>
</RelativeLayout>
就是这么简单,感冒了好难受~(>_<) ,如果你觉得对你有帮助,就帮我点个赞吧!