例如我们用播放器看电影的时候,经常会向前移动进度,SeekBar就是这个功能,它类似一个进度条,但是调节器,可以被用户移动。
例如:
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <SeekBar android:id="@+id/seekbar" android:layout_width="240px" android:layout_height="wrap_content" android:max="500" /> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>Test01.java
package org.hualang.test01; import android.app.Activity; import android.os.Bundle; import android.widget.SeekBar; import android.widget.TextView; public class Test01 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SeekBar seek = (SeekBar)findViewById(R.id.seekbar); seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub ((TextView)findViewById(R.id.text)).setText("Value:"+progress); seekBar.setSecondaryProgress((progress + seekBar.getMax())/2); } }); } }运行结果如下: