老生常谈ProgressBar、ProgessDialog的用法
作者:jingxian 时间:2022-05-02 16:08:58
一、ProgressBar
1. 常用类型
1.1 不确定式圆形进度条
style="@android:style/Widget.Holo.Light.ProgressBar"
style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Large"
...
没有显示进度,可作为过场动画。有大、中、小三种大小,默认为中。
1.2 条形进度条
style="@android:style/Widget.ProgressBar.Horizontal"
style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"
...
带有显示进度。
1.3 标题栏不确定式进度条
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
在标题栏右侧显示的无显示进度的圆形进度条。
1.4 标题栏条形进度条
requestWindowFeature(Window.FEATURE_PROGRESS);
setProgressBarVisibility(true);
在标题栏顶部显示的条形进度条,可通过setProgess(int)设置当前进度,最大值为10000。
2. 常用控件属性
<!--最大显示进度-->
android:max
<!--第一显示进度-->
android:progress
<!--第二显示进度-->
android:secondaryProgress
<!--置是否精确显示;true为不精确,false为精确-->
android:indeterminate
<!--加载自定义样式-->
android:progressDrawable
3. 自定义样式
通过控件的android:progressDrawable属性引用自定义的drawable文件实现。一般需定义三个内容:背景、第一进度、第二进度。
范例:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--背景样式-->
<item android:id="@android:id/background">
<shape>
<!--圆角-->
<corners android:radius="10dip" />
<!--填充色-->
<solid android:color="#dddddd" />
</shape>
</item>
<!--第二进度样式-->
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="10dip" />
<solid android:color="#78bb78" />
</shape>
</clip>
</item>
<!--第一进度样式-->
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="10dip" />
<solid android:color="#55bb55" />
</shape>
</clip>
</item>
</layer-list>
贴张效果图:
4. 关键方法
//设置第一进度
setProgress(int)
//设置第二进度
setSecondaryProgress(int)
//获取第一进度
getProgress()
//获取第二进度
getSecondaryProgress()
//增加或减少第一进度
incrementProgressBy(int)
//增加或减少第二进度
incrementSecondaryProgressBy(int)
//获取进度最大值
getMax()
5. 范例
布局比较简单,线性布局,竖直排列,这里就不贴代码了,直接贴张图:
Java:
public class ProgessBarActivity extends Activity implements View.OnClickListener{
private ProgressBar progressBar;
private TextView text;
private Button addFirst;
private Button addSecond;
private Button subFirst;
private Button subSecond;
private Button reset;
private int first;
private int second;
private int max;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progess_bar);
init();
}
private void init() {
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
text = (TextView) findViewById(R.id.text);
addFirst = (Button) findViewById(R.id.add_first);
subFirst = (Button) findViewById(R.id.sub_first);
addSecond = (Button) findViewById(R.id.add_second);
subSecond = (Button) findViewById(R.id.sub_second);
reset = (Button) findViewById(R.id.reset);
//获取第一、第二、最大进度
first = progressBar.getProgress();
second = progressBar.getSecondaryProgress();
max = progressBar.getMax();
addFirst.setOnClickListener(this);
addSecond.setOnClickListener(this);
subFirst.setOnClickListener(this);
subSecond.setOnClickListener(this);
reset.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_first:
//第一进度加10
progressBar.incrementProgressBy(10);
break;
case R.id.add_second:
//第二进度加10
progressBar.incrementSecondaryProgressBy(10);
break;
case R.id.sub_first:
progressBar.incrementProgressBy(-10);
break;
case R.id.sub_second:
progressBar.incrementSecondaryProgressBy(-10);
break;
case R.id.reset:
//重置为初始数值
progressBar.setProgress(30);
progressBar.setSecondaryProgress(60);
break;
}
//更新文本内容
text.setText("第一进度为" + (int) (1.0*first/max*100) + "%,第二进度为" + (int) (1.0*second/max*100) + "%");
}
}
二、ProgressDialog
1. 构造函数
ProgressDialog(Context context)
ProgressDialog(Context context, int theme)//theme为对话框样式
2. 关键方法
//设置进度条样式
setProgressStyle(int style)
//设置对话框标题
setTitle(String title)
//设置对话框本文信息
setMessage(CharSequence message)
//设置对话框图标
setIcon(Drawable d)
//设置按钮,whichButton为按钮类型,text为按钮名称,listener为 *
setButton(int whichButton, CharSequence text, OnClickListener listener)
//显示对话框
show()
此外,除了这几个方法,ProgressDialog也可使用上面ProgressBar中介绍的方法。
3. 范例
public class ProgressDialogActivity extends Activity {
private ProgressDialog proDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress_dialog);
findViewById(R.id.show).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//新建对话框
proDialog = new ProgressDialog(ProgressDialogActivity.this);
//设置进度条样式
proDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置对话框标题
proDialog.setTitle("初识ProgressDialog");
//设置提示对话框文本
proDialog.setMessage("好好学习,天天向上!");
//设置对话框显示图标
proDialog.setIcon(R.drawable.ic_launcher);
//设置进度条最大进度,默认为10000
proDialog.setMax(100);
//设置初始第一进度
proDialog.incrementProgressBy(30);
//设定取消按钮
proDialog.setButton(DialogInterface.BUTTON_POSITIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
//显示对话框
proDialog.show();
}
});
}
}
标签:ProgressBar,ProgessDialog,用法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java制作简单验证码功能
2022-12-07 02:12:46
![](https://img.aspxhome.com/file/2023/5/60835_0s.jpg)
Android onClick方法与setOnClickListener方法对比
2022-02-09 22:21:40
redis统计APP在线人数的实例
2023-11-24 23:56:54
深入C# 内存管理以及优化的方法详解
2021-10-24 14:24:03
深入浅析Spring 的aop实现原理
2023-01-10 00:00:10
C# 字符串的连接(实例讲解)
2021-11-28 13:57:14
![](https://img.aspxhome.com/file/2023/0/104210_0s.png)
Mybatis打印替换占位符后的完整Sql教程
2023-11-08 22:56:13
![](https://img.aspxhome.com/file/2023/5/97785_0s.jpg)
Springboot2.1.6集成activiti7出现登录验证的实现
2022-08-03 10:44:48
![](https://img.aspxhome.com/file/2023/0/77410_0s.png)
Spring创建Bean的生命周期详析
2022-01-27 06:33:26
![](https://img.aspxhome.com/file/2023/8/71178_0s.png)
Android 获得View宽高的几种方式总结
2021-11-12 05:06:49
![](https://img.aspxhome.com/file/2023/6/101846_0s.png)
SpringMVC RESTFul实现列表功能
2023-10-30 21:15:22
![](https://img.aspxhome.com/file/2023/9/80919_0s.png)
Java使用Lettuce客户端在Redis在主从复制模式下命令执行的操作
2023-11-28 21:38:19
![](https://img.aspxhome.com/file/2023/7/60797_0s.jpg)
SpringBoot中自定义注解实现参数非空校验的示例
2022-04-12 10:55:55
![](https://img.aspxhome.com/file/2023/0/70260_0s.webp)
Android 全屏无标题栏的三种实现方法
2022-01-05 03:01:31
java.lang.NoSuchMethodException: com.sun.proxy.$Proxy58.list错误解决办法
2021-05-25 03:11:19
java中Vector类的常用方法详解
2023-11-26 10:29:07
![](https://img.aspxhome.com/file/2023/4/60234_0s.png)
Java看完秒懂版熔断和降级的关系
2023-11-06 01:12:13
![](https://img.aspxhome.com/file/2023/9/58919_0s.jpg)
WebSocket实现Web聊天室功能
2023-11-27 06:10:52
![](https://img.aspxhome.com/file/2023/9/60189_0s.jpg)
Android RecyclerView选择多个item的实现代码
2022-06-09 10:56:56
![](https://img.aspxhome.com/file/2023/6/117136_0s.jpg)
java压缩zip文件中文乱码问题解决方法
2022-12-20 11:14:25