ProgressBar、ProgessDialog-用法(詳解)
一、ProgressBar
1. 常用類型
1.1 不確定式圓形進(jìn)度條
style="@android:style/Widget.Holo.Light.ProgressBar" style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Large" ...
沒有顯示進(jìn)度,可作為過場動(dòng)畫。有大、中、小三種大小,默認(rèn)為中。
1.2 條形進(jìn)度條
style="@android:style/Widget.ProgressBar.Horizontal" style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal" ...
帶有顯示進(jìn)度。
1.3 標(biāo)題欄不確定式進(jìn)度條
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setProgressBarIndeterminateVisibility(true);
在標(biāo)題欄右側(cè)顯示的無顯示進(jìn)度的圓形進(jìn)度條。
1.4 標(biāo)題欄條形進(jìn)度條
requestWindowFeature(Window.FEATURE_PROGRESS); setProgressBarVisibility(true);
在標(biāo)題欄頂部顯示的條形進(jìn)度條,可通過setProgess(int)設(shè)置當(dāng)前進(jìn)度,最大值為10000。
2. 常用控件屬性
<!--最大顯示進(jìn)度--> android:max <!--第一顯示進(jìn)度--> android:progress <!--第二顯示進(jìn)度--> android:secondaryProgress <!--置是否精確顯示;true為不精確,false為精確--> android:indeterminate <!--加載自定義樣式--> android:progressDrawable
3. 自定義樣式
通過控件的android:progressDrawable屬性引用自定義的drawable文件實(shí)現(xiàn)。一般需定義三個(gè)內(nèi)容:背景、第一進(jìn)度、第二進(jìn)度。
范例:
<?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> <!--第二進(jìn)度樣式--> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="10dip" /> <solid android:color="#78bb78" /> </shape> </clip> </item> <!--第一進(jìn)度樣式--> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="10dip" /> <solid android:color="#55bb55" /> </shape> </clip> </item> </layer-list>
貼張效果圖:
4. 關(guān)鍵方法
//設(shè)置第一進(jìn)度 setProgress(int) //設(shè)置第二進(jìn)度 setSecondaryProgress(int) //獲取第一進(jìn)度 getProgress() //獲取第二進(jìn)度 getSecondaryProgress() //增加或減少第一進(jìn)度 incrementProgressBy(int) //增加或減少第二進(jìn)度 incrementSecondaryProgressBy(int) //獲取進(jìn)度最大值 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); //獲取第一、第二、最大進(jìn)度 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: //第一進(jìn)度加10 progressBar.incrementProgressBy(10); break; case R.id.add_second: //第二進(jìn)度加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: //重置為初始數(shù)值 progressBar.setProgress(30); progressBar.setSecondaryProgress(60); break; } //更新文本內(nèi)容 text.setText("第一進(jìn)度為" + (int) (1.0*first/max*100) + "%,第二進(jìn)度為" + (int) (1.0*second/max*100) + "%"); } }
二、ProgressDialog
1. 構(gòu)造函數(shù)
ProgressDialog(Context context) ProgressDialog(Context context, int theme)//theme為對話框樣式
2. 關(guān)鍵方法
//設(shè)置進(jìn)度條樣式 setProgressStyle(int style) //設(shè)置對話框標(biāo)題 setTitle(String title) //設(shè)置對話框本文信息 setMessage(CharSequence message) //設(shè)置對話框圖標(biāo) setIcon(Drawable d) //設(shè)置按鈕,whichButton為按鈕類型,text為按鈕名稱,listener為監(jiān)聽器 setButton(int whichButton, CharSequence text, OnClickListener listener) //顯示對話框 show()
此外,除了這幾個(gè)方法,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); //設(shè)置進(jìn)度條樣式 proDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //設(shè)置對話框標(biāo)題 proDialog.setTitle("初識ProgressDialog"); //設(shè)置提示對話框文本 proDialog.setMessage("好好學(xué)習(xí),天天向上!"); //設(shè)置對話框顯示圖標(biāo) proDialog.setIcon(R.drawable.ic_launcher); //設(shè)置進(jìn)度條最大進(jìn)度,默認(rèn)為10000 proDialog.setMax(100); //設(shè)置初始第一進(jìn)度 proDialog.incrementProgressBy(30); //設(shè)定取消按鈕 proDialog.setButton(DialogInterface.BUTTON_POSITIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); //顯示對話框 proDialog.show(); } }); } }
以上這篇ProgressBar、ProgessDialog-用法(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Flutter實(shí)現(xiàn)風(fēng)車加載組件的制作
Flutter官方提供了諸如 CircularProgressIndicator和 LinearProgressIndicator兩種常見的加載指示組件,但是說實(shí)話,實(shí)在太普通,所以本文將用Flutter自定義一個(gè)風(fēng)車加載組件,需要的可以參考一下2022-03-03Android SurfaceView基礎(chǔ)用法詳解
這篇文章主要介紹了Android SurfaceView基礎(chǔ)用法詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Unity同步/異步調(diào)用Android的方法實(shí)例
unity在Android端開發(fā)的時(shí)候,免不了要調(diào)用Java,下面這篇文章主要給大家介紹了關(guān)于Unity同步/異步調(diào)用Android的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08Android動(dòng)畫之逐幀動(dòng)畫(Frame Animation)實(shí)例詳解
這篇文章主要介紹了Android動(dòng)畫之逐幀動(dòng)畫(Frame Animation),結(jié)合實(shí)例形式較為詳細(xì)的分析了逐幀動(dòng)畫的原理,注意事項(xiàng)與相關(guān)使用技巧,需要的朋友可以參考下2016-01-01android中實(shí)現(xiàn)在ImageView上隨意畫線涂鴉的方法
今天小編就為大家分享一篇android中實(shí)現(xiàn)在ImageView上隨意畫線涂鴉的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10Android開發(fā)可添加頭尾的RecycleView的實(shí)現(xiàn)
這篇文章主要為大家介紹了Android開發(fā)可添加頭尾的RecycleView的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12