拖动条类似于进度条,但是进度条不可以控制。
拖动条可以被用户控制,在SeekBar中要监听3个事件。
分别是:
1.数值的改变(onProgressBar)
2.开始拖动(onStarTrackingTouch)
3.停止拖动(onStopTrackingTouch)
package com.ko8e;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MyActivity extends Activity {
/** Called when the activity is first created. */
TextView textView1 = null;
TextView textView2 = null;
SeekBar sb = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
sb = (SeekBar) findViewById(R.id.seekBar);
textView1.setText("初始值:" + sb.getProgress());
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "onStopTrackingTouch", Toast.LENGTH_SHORT).show();
}
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "onStartTrackingTouch", Toast.LENGTH_SHORT).show();
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
textView2.setText("当前进度值:" + sb.getProgress());
Toast.makeText(getApplicationContext(), "onProgressChanged", Toast.LENGTH_SHORT).show();
}
});
}
}
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="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30"
android:secondaryProgress="70"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>