软件编程
位置:首页>> 软件编程>> Android编程>> 老生常谈ProgressBar、ProgessDialog的用法

老生常谈ProgressBar、ProgessDialog的用法

作者:jingxian  发布时间:2022-05-02 16:08:58 

标签:ProgressBar,ProgessDialog,用法

一、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>

贴张效果图:

老生常谈ProgressBar、ProgessDialog的用法

4. 关键方法


//设置第一进度
setProgress(int)
//设置第二进度
setSecondaryProgress(int)
//获取第一进度
getProgress()
//获取第二进度
getSecondaryProgress()
//增加或减少第一进度
incrementProgressBy(int)
//增加或减少第二进度
incrementSecondaryProgressBy(int)
//获取进度最大值
getMax()

5. 范例

布局比较简单,线性布局,竖直排列,这里就不贴代码了,直接贴张图:

老生常谈ProgressBar、ProgessDialog的用法

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();
     }
   });
 }
}
0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com