①先來設計一個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.
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>
參考:http://www.eoeandroid.com/thread-1081-1-1.html
留言列表