老生常談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)文章
Android 實(shí)現(xiàn)釘釘自動(dòng)打卡功能
這篇文章主要介紹了Android 實(shí)現(xiàn)釘釘自動(dòng)打卡功能的步驟,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03ListView的Adapter使用(綁定數(shù)據(jù)) 之 自定義每一項(xiàng)的布局去綁定數(shù)據(jù)
之前寫的綁定數(shù)據(jù)是只是簡單的綁定了字符串,這次我們將一次綁定多條數(shù)據(jù)并且嘗試用自定義的布局。在這篇文章中首先講解的是用Hashmap 去綁定數(shù)據(jù),第二個(gè)例子,講解自定義布局然后綁定數(shù)據(jù)2013-06-06Android開發(fā)實(shí)現(xiàn)各種圖形繪制功能示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)各種圖形繪制功能,結(jié)合實(shí)例形式分析了Android圖形繪制常用的組件、函數(shù)使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-09-09Android編程實(shí)現(xiàn)將時(shí)間轉(zhuǎn)化成幾分鐘前、幾天前等形式的工具類
這篇文章主要介紹了Android編程實(shí)現(xiàn)將時(shí)間轉(zhuǎn)化成幾分鐘前、幾天前等形式的工具類,涉及Android針對日期時(shí)間的相關(guān)運(yùn)算與判斷簡單操作技巧,需要的朋友可以參考下2018-02-02Android幀式布局實(shí)現(xiàn)自動(dòng)切換顏色
這篇文章主要為大家詳細(xì)介紹了Android幀式布局實(shí)現(xiàn)自動(dòng)切換顏色,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04android自定義view之實(shí)現(xiàn)日歷界面實(shí)例
本篇文章主要介紹了android自定義view之實(shí)現(xiàn)日歷界面實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Android activity堆棧及管理實(shí)例詳解
這篇文章主要介紹了Android activity堆棧及管理實(shí)例詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,對android activity堆棧相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-09-09Android編程實(shí)現(xiàn)webview執(zhí)行l(wèi)oadUrl時(shí)隱藏鍵盤的workround效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)webview執(zhí)行l(wèi)oadUrl時(shí)隱藏鍵盤的workround效果,較為詳細(xì)的分析了執(zhí)行l(wèi)oadUrl時(shí)隱藏鍵盤的workround具體步驟與兩種實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10