progressBar

多式樣ProgressBar

普通圓形ProgressBar





該類型進度條也就是一個表示運轉的過程,例如發送短信,連接網絡等等,表示一個過程正在執行中。

一般只要在XML佈局中定義就可以了。
  1. <progressBar android:id="@+id/widget43"
  2.       android:layout_width="wrap_content" 
  3.       android:layout_height="wrap_content"    
  4.       android:layout_gravity="center_vertical">
  5. </ProgressBar>
複製代碼
此時,沒有設置它的風格,那麼它就是圓形的,一直會旋轉的進度條。

各大小樣式圓形ProgressBar


超大號圓形ProgressBar


此時,給設置一個style風格屬性後,該ProgressBar就有了一個風格,這裏大號ProgressBar的風格是:
  1. style="?android:attr/progressBarStyleLarge"
複製代碼
完整XML定義是:
  1. <progressBar android:id="@+id/widget196"
  2.       android:layout_width="wrap_content" 
  3.       android:layout_height="wrap_content"
  4.       style="?android:attr/progressBarStyleLarge">
  5. </ProgressBar>
複製代碼


小號圓形ProgressBar




小號ProgressBar對應的風格是:
  1. style="?android:attr/progressBarStyleSmall"
複製代碼
完整XML定義是:
  1. <progressBar android:id="@+id/widget108"
  2.       android:layout_width="wrap_content" 
  3.       android:layout_height="wrap_content"
  4.       style="?android:attr/progressBarStyleSmall">
  5. </ProgressBar>
複製代碼


標題型圓形ProgressBar






標題型ProgressBar對應的風格是:
  1. style="?android:attr/progressBarStyleSmallTitle"
複製代碼
完整XML定義是:
  1. <progressBar android:id="@+id/widget110"
  2.     android:layout_width="wrap_content" 
  3.     android:layout_height="wrap_content"
  4.     style="?android:attr/progressBarStyleSmallTitle">
  5. </ProgressBar>
複製代碼
代碼中實現:
  1. @Override
  2.     protected void onCreate(Bundle savedInstanceState) {
  3.         // TODO Auto-generated method stub
  4.         super.onCreate(savedInstanceState);
  5.         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  6.         //請求窗口特色風格,這裏設置成不明確的進度風格
  7.         setContentView(R.layout.second);
  8.         setProgressBarIndeterminateVisibility(true);
  9.         //設置標題欄中的不明確的進度條是否可以顯示
  10.     }
複製代碼


長形進度條


佈局中的長形進度條


①首先在XML進行佈局
  1. <progressBar android:id="@+id/progressbar_updown"
  2.         android:layout_width="200dp" 
  3.         android:layout_height="wrap_content"
  4.         style="?android:attr/progressBarStyleHorizontal"
  5.         android:layout_gravity="center_vertical" 
  6.         android:max="100"
  7.         android:progress="50"
  8.         android:secondaryProgress="70"    >
複製代碼
講解:
style="?android:attr/progressBarStyleHorizontal"   
設置風格爲長形 
android:max="100"   
最大進度值爲100
android:progress="50"  
初始化的進度值
android:secondaryProgress="70" 
初始化的底層第二個進度值 
android:layout_gravity="center_vertical"   
垂直居中


②代碼中運用
  1. private ProgressBar myProgressBar;
  2. //定義ProgressBar
  3. myProgressBar = (ProgressBar) findViewById(R.id.progressbar_updown);
  4. //ProgressBar通過ID來從XML中獲取
  5. myProgressBar.incrementProgressBy(5);
  6. //ProgressBar進度值增加5
  7. myProgressBar.incrementProgressBy(-5);
  8. //ProgressBar進度值減少5
  9. myProgressBar.incrementSecondaryProgressBy(5);
  10. //ProgressBar背後的第二個進度條 進度值增加5
  11. myProgressBar.incrementSecondaryProgressBy(-5);
  12. //ProgressBar背後的第二個進度條 進度值減少5
複製代碼



頁面標題中的長形進度條



代碼實現:
①先設置一下窗口風格特性
  1. requestWindowFeature(Window.FEATURE_PROGRESS);
  2. //請求一個窗口進度條特性風格
  3. setContentView(R.layout.main);
  4. setProgressBarVisibility(true);
  5. //設置進度條可視
複製代碼
②然後設置進度值
  1. setProgress(myProgressBar.getProgress() * 100);
  2. //設置標題欄中前景的一個進度條進度值
  3. setSecondaryProgress(myProgressBar.getSecondaryProgress() * 100);
  4. //設置標題欄中後面的一個進度條進度值
  5. //ProgressBar.getSecondaryProgress() 是用來獲取其他進度條的進度值
複製代碼


ProgressDialog

ProgressDialog中的圓形進度條
      
ProgressDialog一般用來表示一個系統任務或是開啓任務時候的進度,有一種稍等的意思。
代碼實現:

  1.   ProgressDialog mypDialog=new ProgressDialog(this);
  2.             //實例化
  3.             mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  4.             //設置進度條風格,風格爲圓形,旋轉的
  5.             mypDialog.setTitle("Google");
  6.             //設置ProgressDialog 標題
  7.             mypDialog.setMessage(getResources().getString(R.string.second));
  8.             //設置ProgressDialog 提示信息
  9.             mypDialog.setIcon(R.drawable.android);
  10.             //設置ProgressDialog 標題圖標
  11.             mypDialog.setButton("Google",this);
  12.             //設置ProgressDialog 的一個Button
  13.             mypDialog.setIndeterminate(false);
  14.             //設置ProgressDialog 的進度條是否不明確
  15.             mypDialog.setCancelable(true);
  16.             //設置ProgressDialog 是否可以按退回按鍵取消
  17.             mypDialog.show();
  18.             //讓ProgressDialog顯示
複製代碼



ProgressDialog中的長形進度條
      

代碼實現:
  1. ProgressDialog mypDialog=new ProgressDialog(this);
  2. //實例化
  3.             mypDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  4.             //設置進度條風格,風格爲長形,有刻度的
  5.             mypDialog.setTitle("地獄怒獸");
  6.             //設置ProgressDialog 標題
  7.             mypDialog.setMessage(getResources().getString(R.string.second));
  8.             //設置ProgressDialog 提示信息
  9.             mypDialog.setIcon(R.drawable.android);
  10.             //設置ProgressDialog 標題圖標
  11.             mypDialog.setProgress(59);
  12.             //設置ProgressDialog 進度條進度
  13.             mypDialog.setButton("地獄曙光",this);
  14.             //設置ProgressDialog 的一個Button
  15.             mypDialog.setIndeterminate(false);
  16.             //設置ProgressDialog 的進度條是否不明確
  17.             mypDialog.setCancelable(true);
  18.             //設置ProgressDialog 是否可以按退回按鍵取消
  19.             mypDialog.show();
  20.             //讓ProgressDialog顯示   
複製代碼



AlertDialog.Builder

AlertDialog中的圓形ProgressBar

①先來設計一個Layout,待會兒作爲一個View,加入AlertDialog.Builder
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_gravity="center_horizontal" 
  4.     android:layout_width="wrap_content"
  5.     android:layout_height="wrap_content">
  6.     <LinearLayout android:id="@+id/LinearLayout01"
  7.     android:layout_width="wrap_content" 
  8.     android:layout_height="wrap_content">
  9. </LinearLayout>
  10.     <ProgressBar android:layout_gravity="center_vertical|center_horizontal"
  11.         android:layout_height="wrap_content" 
  12.         android:progress="57"
  13.         android:id="@+id/myView_ProgressBar2" 
  14.         android:layout_width="wrap_content">
  15. </ProgressBar>
  16. </LinearLayout>
複製代碼
②代碼羅
  1. private AlertDialog.Builder AlterD,AlterD2;
  2. //定義提示對話框
  3. private LayoutInflater layoutInflater;
  4. //定義佈局過濾器
  5. private LinearLayout myLayout;
  6. //定義佈局
  7. layoutInflater2=(LayoutInflater) getSystemService(this.LAYOUT_INFLATER_SERVICE);
  8. //獲得系統的佈局過濾服務
  9. myLayout2=(LinearLayout) layoutInflater2.inflate(R.layout.roundprogress, null);
  10. //得到事先設計好的佈局
  11. AlterD2.setTitle(getResources().getString(R.string.RoundO));
  12. //設置對話框標題
  13. AlterD2.setIcon(R.drawable.ma);
  14. //設置對話框圖標
  15. AlterD2.setMessage(getResources().getString(R.string.ADDView));
  16. //設置對話框提示信息
  17. AlterD2.setView(myLayout2);
  18. //設置對話框中的View
  19. AlterD2.show();
  20. //讓對話框顯示
複製代碼


AlertDialog中的長形ProgressBar(可控制)


①先來設計一個Layout,待會兒作爲一個View,加入AlertDialog.Builder
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_gravity="center_horizontal" 
  4. android:layout_width="wrap_content"
  5.     android:layout_height="wrap_content">
  6.     <Button 
  7. android:layout_height="wrap_content" 
  8. android:text="-"
  9.         android:layout_width="50dp" 
  10. android:id="@+id/myView_BT_Down">
  11. </Button>
  12.     <ProgressBar 
  13. android:layout_gravity="center_vertical"
  14.         android:layout_height="wrap_content" 
  15. style="?android:attr/progressBarStyleHorizontal"
  16.         android:id="@+id/myView_ProgressBar" 
  17. android:progress="57"
  18.         android:layout_width="178dp">
  19. </ProgressBar>
  20.     <Button android:layout_height="wrap_content" 
  21. android:text="+"
  22.         android:layout_width="50dp" 
  23. android:id="@+id/myView_BT_Up">
  24. </Button>
  25. </LinearLayout>
複製代碼
②代碼羅
  1. private AlertDialog.Builder AlterD,AlterD2;
  2. //定義提示對話框
  3. private LayoutInflater layoutInflater;
  4. //定義佈局過濾器
  5. private LinearLayout myLayout;
  6. //定義佈局
  7. layoutInflater=(LayoutInflater) getSystemService(this.LAYOUT_INFLATER_SERVICE);
  8. //獲得系統的佈局過濾服務
  9. myLayout=(LinearLayout) layoutInflater.inflate(R.layout.myview, null);
  10. //得到事先設計好的佈局

  11. myup=(Button) myLayout.findViewById(R.id.myView_BT_Up);
  12. mydown=(Button) myLayout.findViewById(R.id.myView_BT_Down);
  13. mypro=(ProgressBar)myLayout.findViewById(R.id.myView_ProgressBar);
  14. //通過myLayout.findViewById來獲取自定義View中的Widget控件元素

  15. myup.setOnClickListener(this);
  16. //設置對話框View中的按鈕監聽器
  17. mydown.setOnClickListener(this);
  18. //設置對話框View中的按鈕監聽器
  19. mypro.setProgress(Tag);
  20. //設置一個Tag作爲進度值
  21. AlterD.setTitle(getResources().getString(R.string.RectO));
  22. //設置對話框標題
  23. AlterD.setIcon(R.drawable.mb);
  24. //設置對話框圖標
  25. AlterD.setMessage(getResources().getString(R.string.ADDView));
  26. //設置對話框提示信息
  27. AlterD.setView(myLayout);
  28. //設置對話框添加的View
  29. AlterD.setPositiveButton("OK", new DialogInterface.OnClickListener(){
  30.     @Override
  31.     public void onClick(DialogInterface dialog, int which) {
  32.         // TODO Auto-generated method stub
  33.         MyProgressBar.Tag=mypro.getProgress();
  34.     }});
  35. //設置對話框按鈕,以及按鈕的事件監聽器
  36. AlterD.show();
  37. //讓對話框顯示
複製代碼
③進度條進度值的按鈕事件
  1. myup.setOnClickListener(this);
  2. //設置對話框View中的按鈕監聽器
  3. mydown.setOnClickListener(this);
  4. //設置對話框View中的按鈕監聽器
  5. 對應的代碼:
  6.     @Override
  7.     public void onClick(View button) {
  8.         // TODO Auto-generated method stub
  9.         SwitchUPorDown(button);
  10.     }

  11.     private void SwitchUPorDown(View button) {
  12.         switch (button.getId()) {
  13.                 case R.id.myView_BT_Up: {
  14.             mypro.incrementProgressBy(1);    
  15.         }
  16.             break;
  17.         case R.id.myView_BT_Down: {
  18.             mypro.incrementProgressBy(-1);
  19.         }
  20.             break;
  21.         default:
  22.             break;
  23.         }
  24.     }
複製代碼


App Widget中的進度條



Widget中的圓形ProgressBar

這個很簡單,在Widget中沒有多大意思,不再敷述。

Widget中的長形ProgressBar(可控制)

Widget的實現就不再重複,假設您已經把Widget佈局,相應設置已經設置好了。也可以在桌面加入類似上面圖中的樣式。
現在我們來實現一下按鈕事件,與進度條的交互。
下面還是簡單講解一下Widget的設計與部署。


①設計Widget佈局
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout 
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:background="@drawable/widget" 
  5.     android:layout_height="74dp" 
  6.     android:layout_width="296dp">
  7.     <Button 
  8.         android:layout_height="wrap_content" 
  9.         android:text="-" 
  10.         android:layout_gravity="center_vertical"
  11.         android:layout_width="50dp" 
  12.         android:id="@+id/widget_BT_Down" 
  13.         android:layout_marginLeft="10dp">
  14.     </Button>
  15.     <ProgressBar 
  16.         android:layout_gravity="center_vertical"
  17.         android:layout_height="wrap_content" 
  18.         style="?android:attr/progressBarStyleHorizontal"
  19.         android:layout_width="178dp" 
  20.         android:id="@+id/widget_ProgressBar">
  21.    </ProgressBar>
  22.     <Button 
  23.         android:layout_height="wrap_content" 
  24.         android:text="+" 
  25.         android:layout_gravity="center_vertical"
  26.         android:layout_width="50dp" 
  27.         android:id="@+id/widget_BT_Up">
  28.    </Button>
  29. </LinearLayout>
複製代碼
②新增一個.res/xml目錄,加入appwidget-provider
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider 
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:initialLayout="@layout/widgetlayout"
  5.     android:updatePeriodMillis="8660000" 
  6.     android:minWidth="296dp"
  7.     android:minHeight="74dp">
  8. </appwidget-provider>
複製代碼
③實現一個AppWidgetProvider子類
  1. package zyf.test.ProgressBar;
  2. import android.appwidget.AppWidgetManager;
  3. import android.appwidget.AppWidgetProvider;
  4. import android.content.Context;
  5. import android.content.Intent;

  6. public class App extends AppWidgetProvider {

  7.     @Override
  8.     public void onEnabled(Context context) {
  9.         // TODO Auto-generated method stub
  10.         super.onEnabled(context);
  11.     }

  12.     @Override
  13.     public void onReceive(Context context, Intent intent) {
  14.         // TODO Auto-generated method stub
  15.         super.onReceive(context, intent);
  16.     }

  17.     @Override
  18.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  19.             int[] appWidgetIds) {
  20.         // TODO Auto-generated method stub
  21.         super.onUpdate(context, appWidgetManager, appWidgetIds);
  22.     }

  23. }
複製代碼
④配置Manifest,進行註冊
  1. <receiver android:name="AppWidget">
  2.     <intent-filter>
  3.     <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
  4.     </intent-filter>
  5.     <meta-data  
  6.         android:resource="@xml/appwidget" 
  7.         android:name="android.appwidget.provider">
  8.     </meta-data>
  9. </receiver>
複製代碼
這裏實現按鈕與進度條的交互。(Widget自己廣播發送與接收)

①按鈕的消息發送
  1. @Override
  2.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  3.             int[] appWidgetIds) {
  4.         // TODO Auto-generated method stub
  5.         final int N = appWidgetIds.length;

  6.         // Perform this loop procedure for each App Widget that belongs to this provider
  7.         for (int i=0; i<N; i++) {
  8.             int appWidgetId = appWidgetIds;
  9.             RemoteViews views=
  10. new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
  11.             
  12.             Intent UPintent=new Intent("zyf.test.widget.UP");
  13.             Intent DOWNintent=new Intent("zyf.test.widget.DOWN");
  14.             //實例化 兩個帶有Action的Intent
  15.             PendingIntent pendingIntentUp
  16. =PendingIntent.getBroadcast(context, 0, UPintent, 0);
  17.             PendingIntent pendingIntentDown
  18. =PendingIntent.getBroadcast(context, 0, DOWNintent, 0);
  19.             //實例化兩個以Intent來構造的PendingIntent
  20.             views.setOnClickPendingIntent(R.id.widget_BT_Up, pendingIntentUp);
  21.             views.setOnClickPendingIntent(R.id.widget_BT_Down, pendingIntentDown);
  22.             //給View上的兩個按鈕綁定事件,這裏是廣播消息的發送
  23.             appWidgetManager.updateAppWidget(appWidgetId, views);
  24.         }
  25.     }
複製代碼
②Widget自身消息接收,使用intent.getAction()來獲取Action
  1. @Override
  2.     public void onReceive(Context context, Intent intent) {
  3.         // TODO Auto-generated method stub
  4.         super.onReceive(context, intent);
  5.         if(intent.getAction().equals("zyf.test.widget.UP")){
  6.             Tag+=5;
  7.             if(Tag>100){
  8.                 Tag=100;
  9.             }
  10.             views.setProgressBar(R.id.widget_ProgressBar, 100, Tag, false);
  11.             appManager.updateAppWidget(thisWidget, views);
  12.         }
  13.         if(intent.getAction().equals("zyf.test.widget.DOWN")){
  14.             Tag-=5;
  15.             if(Tag<0){
  16.                 Tag=0;
  17.             }
  18.             views.setProgressBar(R.id.widget_ProgressBar, 100, Tag, false);
  19.             appManager.updateAppWidget(thisWidget, views);
  20.         }
  21.     }
複製代碼
③進度條的進度值設置
  1. views.setProgressBar(R.id.widget_ProgressBar, 100, Tag, false);
  2. //設置Widget上的進度條的進度值
  3. //第一個參數,Widget上進度條ID
  4. //第二個參數,進度條最大值
  5. //第三個參數Tag,一個int值,就是設置的進度值
  6. //第四個參數,是否是要進度條不確定
複製代碼
注意了,Widget自身的onReceive()方法如果要接收其他的Action廣播。那就必須在Manifest中,在Intent-filter中添加Action:
  1. <receiver android:name="AppWidget">
  2.     <intent-filter>
  3.         <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
  4.                 <action android:name="zyf.test.widget.UP"></action>
  5.                 <action android:name="zyf.test.widget.DOWN"></action>
  6.     </intent-filter>
  7.     <meta-data
  8.         android:resource="@xml/appwidget"
  9.         android:name="android.appwidget.provider">
  10.     </meta-data>
  11. </receiver>
複製代碼
發佈了74 篇原創文章 · 獲贊 0 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章