Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了老生常谈ProgressBar、ProgessDialog的用法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

一、ProgressBar

1. 常用类型

1.1 不确定式圆形进度条

@H_944_17@ style="@android:style/Widget.Holo.Light.ProgressBar" style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Large" ...

没有显示进度,可作为过场动画。有大、中、小三种大小,认为中。

1.2 条形进度条

@H_944_17@ style="@android:style/Widget.ProgressBar.Horizontal" style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal" ...

带有显示进度。

1.3 标题栏不确定式进度条

@H_944_17@ requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setProgressBarIndeterminateVisibility(true);

标题栏右侧显示的无显示进度的圆形进度条。

1.4 标题栏条形进度条

@H_944_17@ requestWindowFeature(Window.FEATURE_PROGRESS); setProgressBarVisibility(true);

标题栏顶部显示的条形进度条,可通过setProgess(int)设置当前进度,最大值为10000

2. 常用控件属性

@H_944_17@ <!--最大显示进度--> 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://scheR_24_11845@as.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. 关键方法

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

5. 范例

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

老生常谈ProgressBar、ProgessDialog的用法

Java:

@H_944_17@ 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. 构造函数

@H_944_17@ ProgressDialog(Context context) ProgressDialog(Context context,int themE)//theme为对话框样式

2. 关键方法

@H_944_17@ //设置进度条样式 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. 范例

@H_944_17@ 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的用法就是小编分享给大家的全部内容了,希望能给大家一个,也希望大家多多支持编程小技巧。

大佬总结

以上是大佬教程为你收集整理的老生常谈ProgressBar、ProgessDialog的用法全部内容,希望文章能够帮你解决老生常谈ProgressBar、ProgessDialog的用法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。