主界面设计:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<SeekBar
android:id="@+id/seekBar"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<Buttonandroid:id="@+id/seekBarButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="获取值"
/>
</LinearLayout>
代码处理:
public class SeekBarActivity extends Activity {
privateSeekBar seekBar;
@Override
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar);
seekBar =(SeekBar) findViewById(R.id.seekBar);
seekBar.setMax(100);//设置最大刻度
seekBar.setProgress(30);//设置当前刻度
seekBar.setOnSeekBarChangeListener(newSeekBar.OnSeekBarChangeListener() {
@Override
publicvoid onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
Log.v("onProgressChanged()", String.valueOf(progress) +", " + String.valueOf(fromTouch));
}
@Override
publicvoid onStartTrackingTouch(SeekBar seekBar) {//开始拖动
Log.v("onStartTrackingTouch()",String.valueOf(seekBar.getProgress()));
}
@Override
publicvoid onStopTrackingTouch(SeekBar seekBar) {//结束拖动
Log.v("onStopTrackingTouch()",String.valueOf(seekBar.getProgress()));
}
});
Buttonbutton = (Button)this.findViewById(R.id.seekBarButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public voidonClick(View v) {
Toast.makeText(SeekBarActivity.this,String.valueOf(seekBar.getProgress()), 1).show();
}
});
}
}