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" tools:context="com.example.android_seekbar.MainActivity" android:orientation="vertical" > <SeekBar android:id="@+id/SeekBar01" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:thumb="@drawable/mythumb" android:progress="50"/> <TextView android:id="@+id/text01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text02" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
main.java
package com.example.android_seekbar; import android.app.Activity; import android.os.Bundle; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; /** * SeekBar:滚动条 * 与ProgressBar的区别在与:拖动条允许用户拖动滑块来改变进度条的大小 * *SeekBar 的主要属性个方法 * SetMax—设置SeekBar的最大数值 * setProgress—设置SeekBar当前的数值 * setSecondaryProgress—设置SeekBar的第二数值,即当前拖动条推荐的数值 * *SeekBar的事件 * 实现接口:SeekBar.OnSeekBarChangeListerer * 数值改变:onProgressChanged * 开始拖动:onStartTrackingTouch * 停止拖动:onStopTrackingTouch * * 自定义进度条: * android:progressDrawable="@drawable/seekBar.img"(改变进度条的样式) * android:thumb="@drawable/thumb"(改变滑块的样式) * * @author Administrator */ 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.SeekBar01); tv1 = (TextView) findViewById(R.id.text01); tv2 = (TextView) findViewById(R.id.text02); seekBar.setOnSeekBarChangeListener(this); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub tv1.setText("正在拖动"); tv2.setText("进度:"+progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub tv1.setText("开始拖动"); } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub tv1.setText("结束拖动"); } }