实现能定点移动的seekbar

布局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:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/seep"
    android:id="@+id/seep"
    />
    
   <SeekBar android:id="@+id/seekbar"
   			android:layout_width="fill_parent"
   			android:layout_height="wrap_content"
   	/>
</LinearLayout>

java代码

public class SeekBarActivity extends Activity {
	
	private SeekBar seekbar;
	private TextView tv ;
	int max=100;
	
	boolean flag = true;
	
	private Handler handler = new Handler(){
		public void handleMessage(Message mes){
			seekbar.setProgress(seekbar.getProgress()+1);
		}
	 
	}; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        seekbar = (SeekBar)findViewById(R.id.seekbar);
        tv = (TextView)findViewById(R.id.seep);
        seekbar.setMax(max);
        seep();
        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				tv.setText("进度为:"+progress+"%");
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				//停止刷新
				flag=false;
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				flag=true;
				handler.sendEmptyMessage(0);
			}
        	
        });
    }	
    
    private void seep(){
    	DisThread dt = new DisThread();
    	dt.start();
    }
   
    class DisThread extends Thread{
    	public void run(){
    		while(flag && seekbar.getProgress()<max){
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			handler.sendEmptyMessage(0);
    		}
    	
    	}
    }
}
 

相关推荐