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

Android Widget開(kāi)發(fā)學(xué)習(xí)教程(二)

移動(dòng)開(kāi)發(fā)
Android Widget開(kāi)發(fā)學(xué)習(xí)教程是本文要介紹的內(nèi)容,主要是來(lái)了解并學(xué)習(xí)Android Widget的應(yīng)用,具體內(nèi)容的詳解來(lái)看本文。

Android Widget開(kāi)發(fā)學(xué)習(xí)教程是本文要介紹的內(nèi)容,主要是來(lái)了解并學(xué)習(xí)Android Widget的應(yīng)用,繼續(xù) Android Widget開(kāi)發(fā)學(xué)習(xí)筆記(一)繼續(xù)講解。本文具體內(nèi)容的實(shí)現(xiàn)來(lái)看本文詳解。

為了實(shí)現(xiàn)一個(gè)手機(jī)Android平臺(tái)的Widget,該Widget中的內(nèi)容是根據(jù)輸入賬號(hào)從嘰歪網(wǎng)站上獲得得。當(dāng)然,這個(gè)過(guò)程需要嘰歪的API,得到信息后進(jìn)行處理并顯示出來(lái)。大體流程就是這樣。好了,進(jìn)入第一步。

該嘰歪賬號(hào)是測(cè)試賬號(hào),用戶名是“students”,密碼是“111111” 請(qǐng)不要擅自更改。

2. 建立一個(gè)Widget

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

要建立一個(gè)Widget,分為如下幾個(gè)步驟:

(1) 創(chuàng)建一個(gè)類,讓其繼承類AppWidgetProvider,在AppWidgetProvider中有許多方法,例如onDelete(Context,int[]),onEnable(Context)等,但一般情況下我們只是覆寫onUpdate(Context,AppWidgetManager,int[])方法。在該方法中,我們啟動(dòng)后臺(tái)服務(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. <receiver android:name="JiwaiWidget"   
  2. android:label="@string/app_name"   
  3. android:icon="@drawable/jiwai">   
  4. <intent-filter>   
  5. <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />   
  6. </intent-filter>   
  7. <meta-data android:name="android.appwidget.provider"   
  8.             android:resource="@xml/info" />   
  9. </receiver>  

對(duì)上面的代碼進(jìn)行解釋:

第一行指定該Widget的接收者是JiwaiWidget,即你建立的AppWidgetProvider子類;

第二行指定該Widget的標(biāo)簽名稱,值為value目錄下string.xml中的app_name值;

第三行指定該Widget的圖標(biāo),值為drawable目錄下jiwai圖片;

第四行-第六行是采用Android文檔中提供的;

第七行指定該Widget的描述者信息,該描述著中定義了Widget的相關(guān)信息,如該Widget的寬度、長(zhǎng)度、自動(dòng)更新的間隔時(shí)間等信息,該描述位于xml目錄下的info.xml中。

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

  1. <appwidget-provider xmlns: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。本例中,需要你的嘀咕帳號(hào)和密碼,所以應(yīng)先顯示一個(gè)Activity,輸入你的賬號(hào)和密碼,然后將得到的信息在你的Widget中顯示。

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

  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <LinearLayout xmlns: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. <LinearLayout android:layout_width="fill_parent"   
  9. android:orientation="horizontal"   
  10. android:layout_height="wrap_content"   
  11. android:background="@drawable/title">   
  12. <TextView android: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. <LinearLayout android:orientation="vertical"   
  23. android:layout_width="fill_parent"   
  24. android:layout_height="fill_parent">   
  25.  
  26. <TextView android: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. <TextView android: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>  

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

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

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns: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. <TextView android: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. <LinearLayout android:orientation="horizontal"   
  15. android:layout_width="fill_parent"   
  16. android:layout_height="wrap_content"   
  17. android:gravity="center_horizontal">   
  18.  
  19. <TextView android: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. <EditText android:id="@+id/username"   
  26. android:layout_width="200px"   
  27. android:layout_height="wrap_content" />   
  28.  
  29. </LinearLayout>   
  30.  
  31. <LinearLayout android:orientation="horizontal"   
  32. android:layout_width="fill_parent"   
  33. android:layout_height="wrap_content"   
  34. android:gravity="center_horizontal">   
  35.  
  36. <TextView android: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. <EditText android:id="@+id/password"   
  43. android:layout_width="200px"   
  44. android:layout_height="wrap_content"   
  45. android:password="true" />   
  46. </LinearLayout>   
  47.  
  48. <LinearLayout android: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>  

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

小結(jié):Android Widget開(kāi)發(fā)學(xué)習(xí)教程的內(nèi)容介紹完了,希望通過(guò)Android Widget內(nèi)容的學(xué)習(xí)能對(duì)你有幫助!

責(zé)任編輯:zhaolei 來(lái)源: iteye
相關(guān)推薦

2011-09-07 13:42:36

Android Wid實(shí)例

2011-09-07 10:34:48

Android Wid

2011-09-07 11:15:25

2011-09-09 16:38:51

Android Wid源碼

2011-09-09 20:14:58

Android Wid

2010-07-13 09:02:19

Widget開(kāi)發(fā)

2011-09-07 16:28:46

QT WidgetQWidget

2010-06-13 09:45:35

Widget開(kāi)發(fā)

2011-09-07 17:54:40

Android Wid開(kāi)發(fā)

2010-07-23 08:54:02

2011-09-09 10:00:20

Android Wid開(kāi)發(fā)

2011-09-07 17:19:16

Web widget

2011-09-08 15:40:45

Android Wid組件

2011-09-08 13:11:07

Android Wid實(shí)例

2011-09-07 14:39:47

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

2011-09-08 09:38:46

HTML5 WidgeDojo

2013-12-26 15:18:09

Android開(kāi)發(fā)安裝開(kāi)發(fā)環(huán)境

2011-09-07 14:25:53

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

2011-09-09 11:05:56

Widget

2011-05-03 15:13:23

BlackBerryWidget
點(diǎn)贊
收藏

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