首页 技术 正文
技术 2022年11月21日
0 收藏 764 点赞 2,169 浏览 4636 个字

一、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();
}
});
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848