自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android Widget開發(fā)詳解

移動(dòng)開發(fā)
Widget開發(fā)有很多值得學(xué)習(xí)的地方,你對他的概念是否熟悉,本文向大家繼續(xù)介紹一下,希望本文的介紹能讓你有所收獲。

本文和大家重點(diǎn)學(xué)習(xí)一下Widget開發(fā)的概念,本例是為了實(shí)現(xiàn)一個(gè)手機(jī)Android平臺的Widget開發(fā),該Widget中的內(nèi)容是根據(jù)輸入賬號從嘰歪網(wǎng)站上獲得得。當(dāng)然,這個(gè)過程需要嘰歪的API,得到信息后進(jìn)行處理并顯示出來。大體流程就是這樣。好了,進(jìn)入***步。

Android Widget開發(fā)系列(二)

該嘰歪賬號是測試賬號,用戶名是“students”,密碼是“111111”請不要擅自更改。

2.建立一個(gè)Widget
Androidreference中有關(guān)于如何建立一個(gè)Widget的詳細(xì)方法,這里簡要說明一下,詳情可以查看AndroidSDK中自帶的reference。

要建立一個(gè)Widget開發(fā)程序,分為如下幾個(gè)步驟:

(1)創(chuàng)建一個(gè)類,讓其繼承類AppWidgetProvider,在AppWidgetProvider中有許多方法,例如onDelete(Context,int[]),onEnable(Context)等,但一般情況下我們只是覆寫onUpdate(Context,AppWidgetManager,int[])方法。在該方法中,我們啟動(dòng)后臺服務(wù)的類,一般是啟動(dòng)Thread類或者Android中的Service類。在該類中我們進(jìn)行從服務(wù)器端獲得數(shù)據(jù)并進(jìn)行處理并在Widget中顯示。

(2)在你的AndroidMenifest.xml中添加一個(gè)receiver標(biāo)簽,讓其指向你的AppWidgetProvider子類。內(nèi)容如下:

  1. <receiverandroid:namereceiverandroid:name="JiwaiWidget" 
  2. android:label="@string/app_name" 
  3. android:icon="@drawable/jiwai"> 
  4. <intent-filter> 
  5. <actionandroid:nameactionandroid:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
  6. </intent-filter> 
  7. <meta-dataandroid:namemeta-dataandroid:name="android.appwidget.provider" 
  8. android:resource="@xml/info"/> 
  9. </receiver> 

對上面的代碼進(jìn)行解釋:
***行指定該Widget開發(fā)的接收者是JiwaiWidget,即你建立的AppWidgetProvider子類;
第二行指定該Widget的標(biāo)簽名稱,值為value目錄下string.xml中的app_name值;
第三行指定該Widget開發(fā)的圖標(biāo),值為drawable目錄下jiwai圖片;
第四行-第六行是采用Android文檔中提供的;
第七行指定該Widget的描述者信息,該描述著中定義了Widget的相關(guān)信息,如該Widget的寬度、長度、自動(dòng)更新的間隔時(shí)間等信息,該描述位于xml目錄下的info.xml中。

(3)編寫你的Widget的provider文件信息(本例中是xml/info.xml)

  1. <appwidget-providerxmlns:androidappwidget-providerxmlns:android="http://schemas.android.com/apk/res/android" 
  2. android:minWidth="200dp" 
  3. android:minHeight="90dp" 
  4. android:updatePeriodMillis="43200000" 
  5. android:initialLayout="@layout/appwidget" 
  6. android:configure="com.lawrenst.jiwai.JiwaiConfigure"> 
  7. </appwidget-provider> 

其中android:updatePeriodMillis是自動(dòng)更新的時(shí)間間隔,android:initialLayout是Widget的界面描述文件。Android:configure是可選的,如果你的Widget需要在啟動(dòng)時(shí)先啟動(dòng)一個(gè)Activity,則需要設(shè)定該項(xiàng)為你的Activity。本例中,需要你的嘀咕帳號和密碼,所以應(yīng)先顯示一個(gè)Activity,輸入你的賬號和密碼,然后將得到的信息在你的Widget中顯示。

(4)在layout目錄下編寫appwidget.xml文件,配置你的Widget的界面信息:

  1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?> 
  2. <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:layout_width="fill_parent" 
  4. android:layout_height="wrap_content" 
  5. android:orientation="vertical" 
  6. android:id="@+id/widget" 
  7. android:background="@drawable/title_a"> 
  8. <LinearLayoutandroid:layout_widthLinearLayoutandroid:layout_width="fill_parent" 
  9. android:orientation="horizontal" 
  10. android:layout_height="wrap_content" 
  11. android:background="@drawable/title"> 
  12. <TextViewandroid:idTextViewandroid:id="@+id/username_display" 
  13. android:textStyle="bold" 
  14. android:layout_width="wrap_content" 
  15. android:layout_height="fill_parent" 
  16. android:textColor="#ffffff" 
  17. android:textSize="15px" 
  18. android:gravity="left|center_vertical" 
  19. android:paddingLeft="6px"/> 
  20. </LinearLayout> 
  21.  
  22. <LinearLayoutandroid:orientationLinearLayoutandroid:orientation="vertical" 
  23. android:layout_width="fill_parent" 
  24. android:layout_height="fill_parent"> 
  25.  
  26. <TextViewandroid:idTextViewandroid:id="@+id/text1" 
  27. android:layout_width="fill_parent" 
  28. android:textColor="#ffffff" 
  29. android:textSize="12px" 
  30. android:gravity="center_vertical|left" 
  31. android:paddingLeft="6px" 
  32. android:layout_height="30px"> 
  33. </TextView> 
  34.  
  35. <TextViewandroid:idTextViewandroid:id="@+id/text2" 
  36. android:textColor="#ffffff" 
  37. android:layout_height="30px" 
  38. android:gravity="center_vertical|left" 
  39. android:textSize="12px" 
  40. android:paddingLeft="6px" 
  41. android:layout_width="fill_parent"> 
  42. </TextView> 
  43. </LinearLayout> 
  44. </LinearLayout> 
  45.  

 


該Widget中包括三個(gè)Textview,兩個(gè)用來顯示嘰歪的信息,一個(gè)用來顯示用戶名,上述代碼比較簡單,故不做解釋。

(5)由于需要一個(gè)Acvivity對象用來輸入賬戶信息,所以在layout目錄下新建一個(gè)login.xml,作為Activity的配置文件:

 

  1. <?xmlversionxmlversion="1.0"encoding="utf-8"?> 
  2. <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:orientation="vertical" 
  4. android:layout_width="fill_parent" 
  5. android:layout_height="fill_parent" 
  6. > 
  7. <TextViewandroid:layout_widthTextViewandroid:layout_width="fill_parent" 
  8. android:layout_height="wrap_content" 
  9. android:text="@string/hello" 
  10. android:textColor="#ff8c00" 
  11. android:capitalize="characters" 
  12. android:textStyle="bold"/> 
  13.  
  14. <LinearLayoutandroid:orientationLinearLayoutandroid:orientation="horizontal" 
  15. android:layout_width="fill_parent" 
  16. android:layout_height="wrap_content" 
  17. android:gravity="center_horizontal"> 
  18.  
  19. <TextViewandroid:layout_widthTextViewandroid:layout_width="wrap_content" 
  20. android:layout_height="wrap_content" 
  21. android:text="@string/user" 
  22. android:textColor="#ff8cff" 
  23. android:capitalize="characters"/> 
  24.  
  25. <EditTextandroid:idEditTextandroid:id="@+id/username" 
  26. android:layout_width="200px" 
  27. android:layout_height="wrap_content"/> 
  28.  
  29. </LinearLayout> 
  30.  
  31. <LinearLayoutandroid:orientationLinearLayoutandroid:orientation="horizontal" 
  32. android:layout_width="fill_parent" 
  33. android:layout_height="wrap_content" 
  34. android:gravity="center_horizontal"> 
  35.  
  36. <TextViewandroid:layout_widthTextViewandroid:layout_width="wrap_content" 
  37. android:layout_height="wrap_content" 
  38. android:text="@string/code" 
  39. android:textColor="#ff8cff" 
  40. android:capitalize="characters"/> 
  41.  
  42. <EditTextandroid:idEditTextandroid:id="@+id/password" 
  43. android:layout_width="200px" 
  44. android:layout_height="wrap_content" 
  45. android:password="true"/> 
  46. </LinearLayout> 
  47.  
  48. <LinearLayoutandroid:orientationLinearLayoutandroid:orientation="horizontal" 
  49. android:layout_width="fill_parent" 
  50. android:layout_height="wrap_content" 
  51. android:gravity="center_horizontal"> 
  52.  
  53. <Button 
  54. android:id="@+id/submit" 
  55. android:layout_width="wrap_content" 
  56. android:layout_height="wrap_content" 
  57. android:text="Submit" 
  58. /> 
  59. </LinearLayout> 
  60. </LinearLayout> 
  61.  

有兩個(gè)EditText用來輸入用戶名和密碼,另外還有一個(gè)Button對象。

準(zhǔn)備工作差不多了,下面就可以寫代碼了。

 

責(zé)任編輯:佚名 來源: javaeye.com
相關(guān)推薦

2011-09-09 20:14:58

Android Wid

2011-09-08 13:11:07

Android Wid實(shí)例

2011-09-07 13:18:40

Android Wid

2011-09-07 10:34:48

Android Wid

2011-02-28 13:04:27

RelativeLayAndroid Wid

2010-07-23 08:54:02

2011-09-09 10:00:20

Android Wid開發(fā)

2011-09-07 17:54:40

Android Wid開發(fā)

2011-09-07 14:39:47

Android Wid設(shè)計(jì)

2011-09-07 13:00:36

2011-09-07 11:15:25

2011-09-08 10:04:07

Windows MobWidget

2011-05-03 15:13:23

BlackBerryWidget

2011-09-07 13:06:04

Android Wid

2011-09-07 14:20:42

Android Wid組件

2010-05-13 10:45:38

2010-07-13 09:08:27

Widget開發(fā)

2011-09-09 19:05:28

Widget

2011-09-07 16:28:46

QT WidgetQWidget

2011-09-13 15:35:40

Widget
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號