SeekBar---可拖动进度条
(1)setMax //设置SeekBar最大数值 (2)setProgress //设置SeekBar当前数值 (3)setSecondaryProgress//设置SeekBar第二数值
监听器:
seekBar = (SeekBar) findViewById(R.id.seekBar); seekBar.setOnSeekBarChangeListener(this); // 数值改变 @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } // 开始拖动 @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } // 停止拖动 @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub }
自定义SeekBar进度条的样式:
android:progressDrawable="@drawable/seekbar_img"//改变进度条的样式 android:thumb="@drawable/thumb"//改变滑块的样式
下面贴上实例工程代码(自定义处仅演示修改改变滑块的样式):
MainActivity.java:
package com.Liuyt.s03_e21_seekbar; import android.app.Activity; import android.os.Bundle; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; 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.main); seekBar = (SeekBar) findViewById(R.id.seekBar); tv1 = (TextView) findViewById(R.id.textView1); tv2 = (TextView) findViewById(R.id.textView2); 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("停止改变"); } }
main.xml:
<?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" > <SeekBar android:thumb="@drawable/my_thumb" android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="300" android:progress="100" /> <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" /> </LinearLayout>
my_thumb.xml (selector 用于自定义滑块的一个效果):
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/select" android:state_focused="true" android:state_window_focused="true"></item> <item android:drawable="@drawable/select" android:state_pressed="true" android:state_window_focused="true"></item> <item android:drawable="@drawable/select" android:state_selected="true" android:state_window_focused="true"></item> <item android:drawable="@drawable/normal"></item> </selector>