首页 > 移动平台 > 详细

android:progressbar实现进度条

时间:2016-02-21 20:01:24      阅读:255      评论:0      收藏:0      [点我收藏+]

在听网络音乐或者下载软件的时候总能看到进度条的身影 它是怎么实现的呢

应该是已下载的大小/总的应用的大小  哈哈 

progress小例子

技术分享

技术分享

代码很简单

就不说名  主要的都有注释

package com.example.progressbar;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private Button button1;
	private Button button2;
	private Button button3;
	private Button show;
	private TextView textView;
	private ProgressBar progress, progress1;
	private ProgressDialog progressDialog;
	private int max;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/*
		 * 启用窗口特征,启用两种形式进度的进度条
		 */
		requestWindowFeature(Window.FEATURE_PROGRESS);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.activity_main);
		/*
		 * 显示两种进度
		 */
		setProgressBarVisibility(true);
		//明确显示进度的
		setProgressBarIndeterminate(false);
		//最大进度为10000
		setProgress(3000);
		//不明确显示进度
		setProgressBarIndeterminateVisibility(true);

		button1 = (Button) findViewById(R.id.add);
		button2 = (Button) findViewById(R.id.sub);
		button3 = (Button) findViewById(R.id.reset);
		show = (Button) findViewById(R.id.show);
		textView = (TextView) findViewById(R.id.textView1);
		progress = (ProgressBar) findViewById(R.id.progressBar);
		init();
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
		show.setOnClickListener(this);

	}
	//初始化
	private void init() {
		// TODO Auto-generated method stub
		max = progress.getMax();
		progress.setProgress(50);
		progress.setSecondaryProgress(60);
		textView.setText("第一进度的百分比为" + (int)((float) progress.getProgress()
				/ (float) max *100)+ "%,第二进度条的百分比为"
				+ (int)((float) progress.getSecondaryProgress() / (float) max *100)+ "%");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		//点击增加按钮  通过incrementProgressBy方法 增加进度
		case R.id.add:
			progress.incrementProgressBy(10);
			progress.incrementSecondaryProgressBy(10);
			break;
		//点击减少按钮  通过incrementProgressBy方法 减少进度
		case R.id.sub:
			progress.incrementProgressBy(-10);
			progress.incrementSecondaryProgressBy(-10);
			break;
		case R.id.reset:
			init();
			break;
		case R.id.show:
			progressDialog=new ProgressDialog(MainActivity.this);
			//设置对话框进度条的图标
			progressDialog.setIcon(R.drawable.ic_launcher);
			//设置对话框进度条的标题
			progressDialog.setTitle("苏苏爱自由");
			//设置对话框进度条的内容
			progressDialog.setMessage("欢迎进入苏苏的博客");
			//设置对话框进度条的显示风格
			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			//不是明确显示进度的
			progressDialog.setIndeterminate(true);
			//添加一个确定按钮 并为其添加事件
			progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Toast.makeText(MainActivity.this, "谢谢大家的支持", Toast.LENGTH_SHORT).show();
				}
			});
			//显示对话框进度条
			progressDialog.show();
			break;
		}
		textView.setText("第一进度的百分比为" + (int)((float) progress.getProgress()
				/ (float) max *100)+ "%,第二进度条的百分比为"
				+ (int)((float) progress.getSecondaryProgress() / (float) max *100)+ "%");
	}

}



android:progressbar实现进度条

原文:http://blog.csdn.net/su20145104009/article/details/50709132

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!