码迷,mamicode.com
首页 > 其他好文 > 详细

进度条简单demo

时间:2015-03-30 16:35:16      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:thread.sleep   progressbar   进度条实时刷新   

布局非常简单。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.loadprogressbardemo.MainActivity" >
    <ProgressBar 
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         style="?android:attr/progressBarStyleHorizontal"
        />
    
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="加载" />


</LinearLayout>

逻辑代码

package com.example.loadprogressbardemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button btn;
	private ProgressBar bar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bar = (ProgressBar)findViewById(R.id.bar);
		btn = (Button)findViewById(R.id.btn);
		
		btn.setOnClickListener(new ButtonListener());
	}
	
	class ButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(v.getId() == R.id.btn){
				Thread t = new LoadThread();
				t.start();
			}
		}
		
	}
	
	class LoadThread extends Thread{
		public void run(){
			for(int i=0;i<100;i++){
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				bar.setProgress(bar.getProgress()+1);
			}
		}
	}
	
}


效果图:


技术分享

进度条简单demo

标签:thread.sleep   progressbar   进度条实时刷新   

原文地址:http://blog.csdn.net/lisineng/article/details/44750773

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!