android中progress进度条的使用

activity.xml:

<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.demo13.MainActivity" >

<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ProgressBar>


<ProgressBar
android:id="@+id/progressBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ProgressBar>


<ProgressBar
android:id="@+id/progressBar3"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ProgressBar>

<ProgressBar
android:id="@+id/progressBar4"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="80" >
</ProgressBar>

<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add" />

<Button
android:id="@+id/reduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reduce" />

<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reset" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

<Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show" />

</LinearLayout>

main.java

package com.example.demo13;

import android.support.v7.app.ActionBarActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 ActionBarActivity implements OnClickListener{

private ProgressBar progress;
private Button add;
private Button reduce;
private Button reset;
private TextView text;
private ProgressDialog prodialog;
private Button show;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);

setContentView(R.layout.activity_main);
setProgressBarVisibility(true);
setProgressBarIndeterminateVisibility(true);
setProgress(600);
init();

}
private void init() {
progress=(ProgressBar) findViewById(R.id.progressBar4);
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);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.add:{
progress.incrementProgressBy(10);
progress.incrementSecondaryProgressBy(10);
break;
}
case R.id.reduce:{
progress.incrementProgressBy(-10);
progress.incrementSecondaryProgressBy(-10);
break;
}
case R.id.reset:{
progress.setProgress(50);
progress.setSecondaryProgress(80);
break;
}
case R.id.show:{
//新建ProgressDialog对象
prodialog=new ProgressDialog(MainActivity.this);
//设置显示风格
prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置标题
prodialog.setTitle("刀冲");
//设置对话框里的文字信息
prodialog.setMessage("欢迎大家访问我的博客");
//设置图标
prodialog.setIcon(R.drawable.ic_launcher);

//设置进度条属性
prodialog.setMax(100);
prodialog.incrementProgressBy(50);
//进度条是明确显示进度的
prodialog.setIndeterminate(false);

//设定一个确定按钮
prodialog.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();
}
});
//可不可以通过返回按钮退出对话框
prodialog.setCancelable(true);

//显示ProgressDialog
prodialog.show();

break;
}
}
text.setText("第一进度百分比"+(int)(progress.getProgress()/(float)progress.getMax()*100)+"% 第二进度百分比"+(int)(progress.getSecondaryProgress()/(float)progress.getMax()*100)+"%");

}



}

原文地址:https://www.cnblogs.com/daochong/p/4943683.html