Android之进度条的使用

进度条也是Android开发中比较常见的控件。

下面介绍2种进度条:

1.默认进度条,这种模式,进度条循环显示动画,这种进度条一般应用于执行的任务的长度不确定。XML属性:android:progressBarStyle。

2.水平进度条,可以呈现操作的进度,它还有一个第二进度条(次要进度条)用来显示中间进度。XML属性:android:progressBarStyleHorizontal。

 

Java:

 

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ProgressBarActivity extends Activity {
	private Button progressbarStartBtn = null;
	private ProgressBar bar1 = null;
	private ProgressBar bar2 = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.progressbar);
		
		progressbarStartBtn = (Button) findViewById(R.id.progressbarStartBtn);
		bar1 = (ProgressBar) findViewById(R.id.progressbar1);
		bar2 = (ProgressBar) findViewById(R.id.progressbar2);
		
		progressbarStartBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 单击开始按钮后,开始进度,同时开始按钮不能使用。
				progressbarStartBtn.setEnabled(false);
				// 设置进度条可见
				bar1.setVisibility(View.VISIBLE);
				bar2.setVisibility(View.VISIBLE);
				
				new Thread(new Runnable() {
					@Override
					public void run() {
						// 每1S更新进度1次。
						while (bar1.getProgress() < bar1.getMax()) {
							// 设置主进度条的进度
							bar1.setProgress(bar1.getProgress() + 10);
							// 设置第二进度条的进度
							bar1.setSecondaryProgress(bar1.getProgress()+10);
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								Log.e(ProgressBarActivity.this.getClass().getName(), "出现异常:" + e.getLocalizedMessage());
								Toast.makeText(ProgressBarActivity.this, "出现异常:" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
							}
						}
						
						// 因为bar2是默认的进度条,上面不会显示进度,所以在这里设置了也毫无意义。
					}
				}).start();
			}
		});
		
	}

}


布局文件progressbar.xml:

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    	<!-- 水平方向的进度条 -->
    	<ProgressBar 
    		android:id="@+id/progressbar1"
    		android:visibility="gone"
    		style="?android:attr/progressBarStyleHorizontal"
    		android:layout_width="200dp"
    		android:layout_height="wrap_content"
    		android:layout_marginLeft="10dip"
    		android:max="100"
    	/>
    	
    	<!-- Android默认的进度条 -->
    	<ProgressBar 
    		android:id="@+id/progressbar2"
    		android:visibility="gone"
    		style="?android:attr/progressBarStyle"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_marginLeft="10dip"
    	/>
    	
    	<Button 
    		android:id="@+id/progressbarStartBtn"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:text="@string/start"
    	/>
</LinearLayout>

还有在AndroidMaifest.xml中注册Activity:
<activity android:name=".ProgressBarActivity"></activity>


这个程序中,总共有2个进度条,1个水平进度条,一个默认的进度条。最开始时事不可见的。

 

还有一个开始按钮,当点击开始按钮后将2个进度条设置为可见,同时启动一个线程每1S更新水平进度条1次。

显示的效果:

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值