android控件之progressbar

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="278dp"
        android:visibility="gone"
        android:layout_gravity="center_vertical"
        android:max="100" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        />
	<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:orientation="horizontal" >
	    <Button
	        android:id="@+id/button1"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="启动进度条" />
	
	    <Button
	        android:id="@+id/button2"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="停止进度条" />
	</LinearLayout>
</LinearLayout>

ProgressbarActivity.java

package com.android.forlinx;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class ProgressbarActivity extends Activity {
	private ProgressBar progressbar = null;
	private Button start = null ,stop = null;
	private Handler handler = new Handler();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        progressbar = (ProgressBar)findViewById(R.id.progressBar2);
        start = (Button)findViewById(R.id.button1);
        stop = (Button)findViewById(R.id.button2);
        
        progressbar.setProgress(0);
        start.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				handler.post(runnable);
				
			}
		});
        stop.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				handler.removeCallbacks(runnable);
				
			}
		});   
    }
    int progress = 0;
    Runnable runnable = new Runnable() {
		
		public void run() {
			progressbar.setVisibility(View.VISIBLE);
			progress = progressbar.getProgress()+10;
			progressbar.setProgress(progress);
			if (progress < 100){
				handler.postDelayed(runnable, 100);
			}else{
				 progressbar.setVisibility(View.GONE);
	                startActivity(new Intent(ProgressbarActivity.this, TestActivity.class));
	                handler.removeCallbacks(runnable);
	                progressbar.setProgress(0);
				
			}
				
			
		}
	};
}

效果图




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值