安卓课程二十一 SeekBar拖动控件的使用

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"   >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textIsSelectable="true" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textIsSelectable="true" />

    <SeekBar
        android:id="@+id/seekBar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30"
        android:secondaryProgress="60" />

</LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnSeekBarChangeListener{

	private TextView tv1,tv2;
	private SeekBar sb1,sb2;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		tv1 = (TextView) this.findViewById(R.id.textview1);
		tv2 = (TextView) this.findViewById(R.id.textview2);
		sb1 = (SeekBar) this.findViewById(R.id.seekBar1);
		sb2 = (SeekBar) this.findViewById(R.id.seekBar2);
		sb1.setOnSeekBarChangeListener(this);
		sb2.setOnSeekBarChangeListener(this);
	}

	 /***
	  * 当滑动滑竿的时候会触发的事件
	  */
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		 if(seekBar.getId() == R.id.seekBar1){
			 tv1.setText("seekBar1 当前位置是:"+progress) ;
		 }else {
			 tv2.setText("seekBar2 当前位置是:"+progress) ;
		 }
		
	}

	/**
	 * 当开始拖动滑竿时触发
	 */
	public void onStartTrackingTouch(SeekBar seekBar) {
		int positon = seekBar.getProgress();
		if(seekBar.getId() == R.id.seekBar1){
			 tv1.setText("开始拖动seekBar1 ,当前位置是:"+positon) ;
		 }else {
			 tv2.setText("开始拖动seekBar2,当前位置是:"+positon) ;
		 }
		
	}
	/**
	 * 当结束拖动滑竿时触发
	 */
	public void onStopTrackingTouch(SeekBar seekBar) {
		int positon = seekBar.getProgress();
		if(seekBar.getId() == R.id.seekBar1){
			 tv1.setText("结束拖动seekBar1 ,当前位置是:"+positon) ;
		 }else {
			 tv2.setText("结束拖动seekBar2,当前位置是:"+positon) ;
		 }
	}
}

相关推荐