18 Android ProgressBar

package com.progress.test;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class ProgressBarActivity extends Activity {
    /** Called when the activity is first created. */
	private ProgressBar progreeBar;
	private Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        progreeBar=(ProgressBar)findViewById(R.id.progressBar1);
        btn=(Button)findViewById(R.id.button1);
        progreeBar.setMax(100);
        btn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new MyTask().execute();
			}
		});
        
        
        
        
    }
    
    class MyTask extends AsyncTask<Void, Integer, Void>
    {
    	@Override
    	protected void onProgressUpdate(Integer... values) {
    		// TODO Auto-generated method stub
    		super.onProgressUpdate(values);
    		progreeBar.setProgress(values[0]);
    	}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			int i=1;
			while(i<1000)
			{
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				publishProgress(i);
				i++;
			}
			return null;
		}
    	
    }
}

你可能感兴趣的:(18 Android ProgressBar)