android SeekBar(带滑动块的进度条)的应用举例

android SeekBar(带滑动块的进度条)的应用举例_第1张图片

AppMain.java

package lxy.listsoft;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;

public class AppMain extends Activity {

	private TextView textView;
	private SeekBar seekBar;
	int seekBarProgress;
	MyThread thread;
	boolean key = true;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textView = (TextView)findViewById(R.id.testView01);
        seekBar = (SeekBar)findViewById(R.id.seekBar01);
        seekBar.setMax(200);
        seekBar.setOnSeekBarChangeListener(new SeekBarListener());
    }
    
	protected void onResume() {
		super.onResume();
		
		thread = new MyThread();
		thread.start();
	}

	//SeekBar的监听器
    class SeekBarListener implements SeekBar.OnSeekBarChangeListener{

    	//当进度改变时,参数fromUser判断是不是进度的改变由用户手动引起
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			if(fromUser){
				Log.d("test", ""+progress);
				seekBarProgress = progress;
			}
			textView.setText(""+progress);
		}

		//当用户开始滑动时
		public void onStartTrackingTouch(SeekBar seekBar) {
			key = false;
			Log.d("test", "myThread stop");
		}

		//当用户结束滑动时
		public void onStopTrackingTouch(SeekBar seekBar) {
			Log.d("test", "myThread isAlive"+thread.isAlive());
			key = true;
			thread = new MyThread();
			thread.start();
			Log.d("test", "myThread isAlive"+thread.isAlive());
		}
    }
    
    class MyThread extends Thread{
		public void run() {
			super.run();
			
			while(key){
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				seekBarProgress++;
				if(seekBarProgress > 200){
					seekBarProgress = 0;
				}
				seekBar.setProgress(seekBarProgress);
			}
		}
    }
}

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"
    >
<TextView  
	android:id="@+id/testView01"
	android:textSize="20pt"
	android:gravity="center_vertical|center_horizontal"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="0"/>
<SeekBar
	android:id="@+id/seekBar01"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"></SeekBar>
</LinearLayout>


你可能感兴趣的:(thread,android,layout,Class,encoding)