/res/layout/main.xml代码如下:
- <?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/mySeekBar"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:max="100"
- android:progress="30"
- android:secondaryProgress="50"/>
- <TextView
- android:id="@+id/myProgress"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <TextView
- android:id="@+id/myTracking"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
Java代码如下:
- package com.demo.android.ui;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.SeekBar;
- import android.widget.TextView;
- public class SeekBarActivity extends Activity {
- private TextView myProgress;
- private TextView myTracking;
- private SeekBar mySeekBar;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- myProgress=(TextView) findViewById(R.id.myProgress);
- myTracking=(TextView) findViewById(R.id.myTracking);
- mySeekBar=(SeekBar) findViewById(R.id.mySeekBar);
- mySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
- /**
- * 停止拖动拖动条时触发该方法
- */
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- myTracking.setText("TrackingTouch stop....");
- }
- /**
- * 开始拖动拖动条时触发该方法
- */
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- myTracking.setText("TrackingTouch start....");
- }
- /**
- * 拖动条改变时触发该事件
- * 参数一:当前操作的拖动条
- * 参数二:当前拖动条的值
- */
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
- myProgress.setText(progress+" onProgressChanged = "+fromUser);
- }
- });
- }
- }