Android的ProgressBar

  注意点:

必须在setContentView 前面设置,否则会报错。

重要的方法:

  progress.incrementProgressBy(int diff);//参数为进度数,进度满了为100.不能够超过100.

  progress.incrementSecondaryProgressBy(-10);

package com.wyl.progressbartest;

import android.app.Activity;
import android.os.Bundle;
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;

public class MainActivity extends Activity implements OnClickListener{
	Button btn01;
	Button btn02;
	TextView tv;
	ProgressBar pb01;
	ProgressBar pb02;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/*
		 * 启用窗口特征,
		 * 1. 带进度的进度条
		 * 2. 不带进度的进度条
		 * 必须在setContentView 前面设置,否则会报错。
		 */
		System.out.println("=======的对肌肤健康========");
		requestWindowFeature(Window.FEATURE_PROGRESS);//带进度的
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);//不带进度
		setContentView(R.layout.activity_main);
		init();
		
		
	}
	/**
	 * 初始化一些控件
	 */
	public void init() {
//		// TODO Auto-generated method stub
//		progress = (ProgressBar) findViewById(R.id.horiz);
//		add = (Button) findViewById(R.id.add);
//		reduce = (Button) findViewById(R.id.reduce);
//		reset = (Button) findViewById(R.id.reset);
//		text = (TextView) findViewById(R.id.text);
//		show=(Button) findViewById(R.id.show);
//		show.setOnClickListener(this);
//		// 获取第一进度条的进度
//		int first = progress.getProgress();
//		// 获取第二进度条的进度
//		int second = progress.getSecondaryProgress();
//		// 获取进度条的最大进度
//		int max = progress.getMax();
//		text.setText("第一进度百分比:" + (int) (first / (float) max * 100)
//				+ "% 第二进度百分比:" + (int) (second / (float) max * 100) + "%");
//		add.setOnClickListener(this);
//		reduce.setOnClickListener(this);
//		reset.setOnClickListener(this);
		pb02 = (ProgressBar) findViewById(R.id.progressBar2);
		int first = pb02.getProgress();
		System.out.println("==first:=="+first);
		int second = pb02.getSecondaryProgress();
		btn01 = (Button) findViewById(R.id.button1);
		btn02 = (Button) findViewById(R.id.button2);
		tv = (TextView) findViewById(R.id.textView1);
		btn01.setOnClickListener(this);
		btn02.setOnClickListener(this);
		
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			// 增加第一进度和第二进度10个刻度
//			progress.incrementProgressBy(10);
//			progress.incrementSecondaryProgressBy(10);
			pb02.incrementProgressBy(-10);
			System.out.println("===减少进度10===");
			pb02.incrementSecondaryProgressBy(-10);
			tv.setText("第一进度:"+pb02.getProgress());
			tv.setText(tv.getText().toString()+",第二进度条"+pb02.getSecondaryProgress());
			break;
		case R.id.button2:
			// 增加第一进度和第二进度10个刻度
//			progress.incrementProgressBy(10);
//			progress.incrementSecondaryProgressBy(10);
			pb02.incrementProgressBy(10);
			System.out.println("====增加进度10===");
			pb02.incrementSecondaryProgressBy(10);
			tv.setText("第一进度:"+pb02.getProgress());
			tv.setText(tv.getText().toString()+",第二进度条"+pb02.getSecondaryProgress());
			break;
		default:
			break;
		}
	}

}

  

原文地址:https://www.cnblogs.com/Sunnor/p/4703964.html