哪些年遇到過的Andriod問題(7)ProgressBar 史上最強大講解

多式樣ProgressBar

普通圓形ProgressBar


1.png




2.png






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

一般只要在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

3.png



此時,給設置一個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

4.png




小號ProgressBar對應的風格是:
 

  1. style="?android:attr/progressBarStyleSmall"

標題型圓形ProgressBar

5.png




6.png




標題型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.     }

長形進度條

佈局中的長形進度條

7.png



①首先在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"    >

講解: 111.png

②代碼中運用
 

  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

頁面標題中的長形進度條

8.png


代碼實現:
①先設置一下窗口風格特性
 

  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中的圓形進度條
9.png


10.png



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中的長形進度條


11.png

12.png




代碼實現:
13.png

  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


14.png

  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



15.png

  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.  
  12. myup=(Button) myLayout.findViewById(R.id.myView_BT_Up);
     
  13. mydown=(Button) myLayout.findViewById(R.id.myView_BT_Down);
     
  14. mypro=(ProgressBar)myLayout.findViewById(R.id.myView_ProgressBar);
     
  15. //通過myLayout.findViewById來獲取自定義View中的Widget控件元素
     

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


③進度條進度值的按鈕事件

  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.  
  12.     private void SwitchUPorDown(View button) {
     
  13.         switch (button.getId()) {
     
  14.                 case R.id.myView_BT_Up: {
     
  15.             mypro.incrementProgressBy(1);    
     
  16.         }
     
  17.             break;
     
  18.         case R.id.myView_BT_Down: {
     
  19.             mypro.incrementProgressBy(-1);
     
  20.         }
     
  21.             break;
     
  22.         default:
     
  23.             break;
     
  24.         }
     
  25.     }

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.  
  7. public class App extends AppWidgetProvider {
     

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

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

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

  27.  
  28. }


④配置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.  
  7.         // Perform this loop procedure for each App Widget that belongs to this provider
     
  8.         for (int i=0; i<N; i++) {
     
  9.             int appWidgetId = appWidgetIds;
     
  10.             RemoteViews views=
     
  11. new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
     
  12.             
     
  13.             Intent UPintent=new Intent("zyf.test.widget.UP");
     
  14.             Intent DOWNintent=new Intent("zyf.test.widget.DOWN");
     
  15.             //實例化 兩個帶有Action的Intent
     
  16.             PendingIntent pendingIntentUp
     
  17. =PendingIntent.getBroadcast(context, 0, UPintent, 0);
     
  18.             PendingIntent pendingIntentDown
     
  19. =PendingIntent.getBroadcast(context, 0, DOWNintent, 0);
     
  20.             //實例化兩個以Intent來構造的PendingIntent
     
  21.             views.setOnClickPendingIntent(R.id.widget_BT_Up, pendingIntentUp);
     
  22.             views.setOnClickPendingIntent(R.id.widget_BT_Down, pendingIntentDown);
     
  23.             //給View上的兩個按鈕綁定事件,這裏是廣播消息的發送
     
  24.             appWidgetManager.updateAppWidget(appWidgetId, views);
     
  25.         }
     
  26.     }


②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>

發佈了54 篇原創文章 · 獲贊 19 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章