zoukankan      html  css  js  c++  java
  • android handler msg的使用 实现进度条

    package com.app.threadtest;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ProgressBar;
    
    public class MyActivity extends Activity {
    	//private static final int PROGRESS = 0x1;
    
    	private ProgressBar mProgress;
    	private Button mButton;
    	private int mProgressStatus = 0;
    
    	private Handler mHandler = new Handler();
    	
    	protected void onCreate(Bundle icicle) {
    		super.onCreate(icicle);
    
    		setContentView(R.layout.progressbar_activity);
    		mProgress = (ProgressBar) findViewById(R.id.progress_bar);
    		
    		// 按钮事件
    		mButton = (Button) findViewById(R.id.button1);
    		mButton.setOnClickListener(new View.OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    				
    				// Start lengthy operation in a background thread
    				new Thread(new Runnable() {
    					public void run() {
    						while (mProgressStatus < 100) {
    							mProgressStatus = doWork();
    
    							// Update the progress bar
    							mHandler.post(new Runnable() {
    								public void run() {
    									mProgress.setProgress(mProgressStatus);
    								}
    							});
    						}
    					}
    				}).start();
    			}
    		});
    	}
    	protected int doWork() {
    		mProgressStatus++;
    		return mProgressStatus;
    	}
    }

  • 相关阅读:
    编程题目: PAT 1012. 数字分类 (20)
    编程题目: PAT 1011. A+B和C (15)
    Poj3683(2-set
    LightOJ 1427(AC自动机
    UVA 11990(BIT套treap
    网络流汇总....
    10月——备战区域赛
    去掉ubuntu的文件管理器中“位置”栏里的无用标签
    Poj 2104(主席树入门
    Poj 1568(极大极小搜索
  • 原文地址:https://www.cnblogs.com/aikongmeng/p/3697409.html
Copyright © 2011-2022 走看看