先介绍一下ProgressBar几种比较常用的属性
布局中设置:
android:max="100" ——最大显示进度
android:progress="50"——第一显示进度
android:secondaryProgress="80"——第二显示进度
android:indeterminate="true"——设置是否精确显示,true表示不精确显示进度,false表示精确显示进度
使用Java代码设置:
setProgress(int) //设置第一进度
setSecondaryProgress(int) //设置第二进度
getProgress() //获取第一进度
getSecondaryProgress() //获取第二进度
incrementProgressBy(int) //增加或减少第一进度
incrementSecondaryProgressBy(int) //增加或减少第二进度
getMax() //获取最大进度
对普通进度条和提示框进度条就不详细说明了,后面有一个例子,会有几种进度条的使用方法,在代码中有详细的注释。这里介绍一下自定义进度条的实现,以水平进度条为例。
1、在布局文件中的style属性就是设置进度条样式的
2、实际上面的背景文件是位于@android:style/Widget.ProgressBar.Horizontal,既上面的布局可以写成
3、查看系统中的水平进度条风格文件
4、上面的android:progressDrawable属性是设置进度条背景,进入查看
-
-
-
5、可以看到,上面文件中的3个item标签分别是设置:进度条、第二进度条、第一进度条的背景色。这里我们在drawable文件夹下新建一个progress_bar.xml文件,将上面的代码复制进来,并修改背景色。注意:最外层标签是否一致,我在复制时只复制了3个item,结果总是报错,找了半天才找到原因。
-
-
-
6、在布局文件中设置自定义背景增加android:progressDrawable="@drawable/progress_bar"属性设置
上面的自定义进度条只是修改了一下背景颜色,如果同时修改其他属性,还可以将进度条风格也在自己的style.xml文件中重新定义使用。
下面是一个完整的进度条使用代码,注释比较详细,自定义进度条直接使用上面的progress_bar的设置。
1、布局文件
2、Java代码中进度条功能实现
package com.cx.testdemo;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
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 android.view.View.OnClickListener{
private ProgressBar progress;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private TextView textView;
private ProgressDialog progressDialog;
@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);
setProgressBarIndeterminateVisibility(true);
//设置带进度条刻度,最大值为10000
setProgress(600);
findView();
}
private void findView() {
// TODO Auto-generated method stub
progress = (ProgressBar) findViewById(R.id.progressBar4);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
textView = (TextView) findViewById(R.id.textView1);
init();
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
}
private void init() {
//获取第一进度条进度
int first = progress.getProgress();
//获取第二进度条进度
int second = progress.getSecondaryProgress();
//获取进度条最大进度
int max = progress.getMax();
textView.setText("第一进度百分比:" + (int)(first/(float)max*100) + "% 第二进度百分比:" + (int)(second/(float)max*100));
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
//增加第一进度和第二进度10刻度
progress.incrementProgressBy(10);
progress.incrementSecondaryProgressBy(10);
break;
case R.id.button2:
//减少第一进度和第二进度10刻度
progress.incrementProgressBy(-10);
progress.incrementSecondaryProgressBy(-10);
break;
case R.id.button3:
progress.setProgress(50);
progress.setSecondaryProgress(80);
break;
case R.id.button4:
/**
* 页面显示风格
*/
//新建ProgressDialog对象
progressDialog = new ProgressDialog(MainActivity.this);
//设置显示风格
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置标题
progressDialog.setTitle("提示");
//设置对话框内信息
progressDialog.setMessage("当前进度");
//设置图标
progressDialog.setIcon(R.drawable.ic_launcher);
/**
* 页面ProgressDialog的一些属性
*/
//设置最大进度
progressDialog.setMax(100);
//设置初始化已经增长到的进度
progressDialog.incrementProgressBy(50);
//进度条是精确显示进度的
progressDialog.setIndeterminate(false);
//确定按钮(按钮类型,显示内容,点击事件)
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.setCancelable(true);
//显示ProgressDialog
progressDialog.show();
break;
}
init();
}
}
源码下载