android控件之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/mySeekBar" 
	    android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:max="100"
		android:progress="25" 
		android:secondaryProgress="50"/>
	<TextView 
	    android:id="@+id/TextView1" 
	    android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="" />
	<TextView 
	    android:id="@+id/TextView2" 
	    android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="" />
</LinearLayout>

MytestActivity.java
package com.android.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MytestActivity extends Activity {
    /** Called when the activity is first created. */
	private SeekBar seekBar;
	private TextView textView1,textView2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        seekBar = (SeekBar)findViewById(R.id.mySeekBar);
        textView1 = (TextView)findViewById(R.id.TextView1);
        textView2 = (TextView)findViewById(R.id.TextView2);
        seekBar.setOnSeekBarChangeListener(SeekBarListener);//添加事件监听
    }
    private OnSeekBarChangeListener SeekBarListener = new OnSeekBarChangeListener()
    {

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// TODO Auto-generated method stub
			textView1.setText("当前值:"+progress);
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			textView2.setText("拖动中...");
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			textView2.setText("拖动完毕");
		}
    };
}

结果如图



你可能感兴趣的:(android控件之SeekBar)