![安卓開發(fā)實例一_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/12/7fd38525-ffdd-4dee-a167-c2378415a880/7fd38525-ffdd-4dee-a167-c2378415a8801.gif)
![安卓開發(fā)實例一_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/12/7fd38525-ffdd-4dee-a167-c2378415a880/7fd38525-ffdd-4dee-a167-c2378415a8802.gif)
![安卓開發(fā)實例一_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/12/7fd38525-ffdd-4dee-a167-c2378415a880/7fd38525-ffdd-4dee-a167-c2378415a8803.gif)
![安卓開發(fā)實例一_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/12/7fd38525-ffdd-4dee-a167-c2378415a880/7fd38525-ffdd-4dee-a167-c2378415a8804.gif)
![安卓開發(fā)實例一_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/12/7fd38525-ffdd-4dee-a167-c2378415a880/7fd38525-ffdd-4dee-a167-c2378415a8805.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、開卷語 俗話說,“熟讀唐詩三百首,不會作詩也會吟”。最近收集了很多Android的示例代碼,從這些代碼的閱讀和實驗中學習到很多知識,從而產(chǎn)生寫這個系列的打算,目標就是一步步跟著實例進行動手實作,真正從“做”中體會和學習Android開發(fā)。本文是這個系列的第一篇,目標是Android自帶的一個范例程序:記事本,將分為四篇文章進行詳細介紹。預備知識 搭建開發(fā)環(huán)境,嘗試編寫”Hello World”,了解Android的基本概念,熟悉Android的API(官方文檔中都
2、有,不贅述)。程序截圖 先來簡單了解下程序運行的效果程序入口點 類似于win32程序里的WinMain函數(shù),Android自然也有它的程序入口點。它通過在AndroidManifest.xml文件中配置來指明,可以看到名為NotesList的activity節(jié)點下有這樣一個intent-filter,其action為ent.action.MAIN, Category指定為 ent.category.LAUNCHER,這就指明了這個activity是作為入口activity,系統(tǒng)查找
3、到它后,就會創(chuàng)建這個activity實例來運行,若未發(fā)現(xiàn)就不啟動(你可以把MAIN改名字試試)。 <intent-filter> <action android:name="ent.action.MAIN" />
4、60; <category android:name="ent.category.LAUNCHER" /> </intent-filter>NotesList詳解 就從入口點所在的activity(見圖1)開始,可以
5、看到這個activity最重要的功能就是顯示日志列表。這個程序的日志都存放在Sqlite數(shù)據(jù)庫中,因此需要讀取出所有的日志記錄并顯示。先來看兩個重要的私有數(shù)據(jù),第一個PROJECTION字段指明了“日志列表“所關注的數(shù)據(jù)庫中的字段(即只需要ID和Title就可以了)。 private static final String PROJECTION = new String
6、160; Notes._ID, / 0 Notes.TITLE, / 1 第二個字段COLUMN_INDEX_TITLE指明title字段在數(shù)據(jù)表中的索引。private static final int COLUMN_INDEX_TITLE =
7、160;1;然后就進入第一個調(diào)用的函數(shù)onCreate。 Intent intent = getIntent(); if (intent.getData() = null)
8、 intent.setData(Notes.CONTENT_URI); 因為NotesList這個activity是系統(tǒng)調(diào)用的,此時的intent是不帶數(shù)據(jù)和操作類型的,系統(tǒng)只是在其中指明了目標組件是Notelist,所以這里把”content:/ vider.NotePad/notes”保存到intent里面,這個URI地址指明了數(shù)
9、據(jù)庫中的數(shù)據(jù)表名(參見以后的NotePadProvider類),也就是保存日志的數(shù)據(jù)表notes。 Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, null, null, Notes.DEFAULT_SORT_ORDER); 然后調(diào)用managedQuery函數(shù)查詢出所有的日志信息,這
10、里第一個參數(shù)就是上面設置的” content:/ vider.NotePad/notes”這個URI,即notes數(shù)據(jù)表。PROJECTION 字段指明了結(jié)果中所需要的字段,Notes.DEFAULT_SORT_ORDER 指明了結(jié)果的排序規(guī)則。實際上managedQuery并沒有直接去查詢數(shù)據(jù)庫,而是通過Content Provider來完成實際的數(shù)據(jù)庫操作,這樣就實現(xiàn)了邏輯層和數(shù)據(jù)庫層的分離。 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,&
11、#160;R.layout.noteslist_item, cursor, new String Notes.TITLE , new int android.R.id.text1 ); setListAdapte
12、r(adapter); 查詢出日志列表后,構造一個CursorAdapter,并將其作為List View的數(shù)據(jù)源,從而在界面上顯示出日志列表??梢钥吹?,第二個參數(shù)是,打開對應的noteslist_item.xml文件,<TextView xmlns:android=" android:id="android:id/text1" android:layout_width="fil
13、l_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center_vertical" android:
14、paddingLeft="5dip" android:singleLine="true"/>就是用來顯示一條日志記錄的TextView,最后兩個字段指明了實際的字段映射關系,通過這個TextView來顯示一條日志記錄的title字段。處理“選擇日志”事件 既然有了“日志列表”,就自然要考慮如何處理某一條日志的單擊事件,這通過重載onListItemClick方法來完成, Override
15、 protected void onListItemClick(ListView l, View v, int position, long id) Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);
16、160; String action = getIntent().getAction(); if (Intent.ACTION_PICK.equals(action) | Intent.ACTION_GET_CONTENT.equals(action)
17、160; / The caller is waiting for us to return a note selected by / the user. The have clicked
18、0;on one, so return it now. setResult(RESULT_OK, new Intent().setData(uri); else &
19、#160; / Launch activity to view/edit the currently selected item startActivity(new Intent(Intent.ACTION_EDIT, uri);
20、; 首先通過”content:/ vider.NotePad/notes”和日志的id 號拼接得到選中日志的真正URI,然后創(chuàng)建一個新的Intent,其操作類型為Intent.ACTION_EDIT,數(shù)據(jù)域指出待編輯的日志URI(這里只分析else塊)。Intent深度剖析那么,上面這句startActivity(new Intent(Intent.ACTION_EDIT, uri)執(zhí)行后會發(fā)生什么事情呢?這時候Android系統(tǒng)就跳出來接管了,它會根據(jù)intent中的信息找到對應的
21、activity,在這里找到的是NoteEditor這個activity,然后創(chuàng)建這個activity的實例并運行。那么,Android又是如何找到NoteEditor這個對應的activity的呢?這就是intent發(fā)揮作用的時刻了。new Intent(Intent.ACTION_EDIT, uri)這里的Intent.ACTION_EDIT=” ent.action.EDIT”,另外通過設置斷點,我們看下這里的uri值: 可以看到選中的日志條目的URI是:。然后我們再來看下An
22、droidmanfest.xml,其中有這個provider<provider android:name="NotePadProvider" android:authorities="vider.NotePad" />
23、0; 發(fā)現(xiàn)沒有?它也有,這個是的一部分,同時 <activity android:name="NoteEditor" android:theme="android:style/Theme.Light" a
24、ndroid:label="string/title_note" android:screenOrientation="sensor" android:configChanges="keyboardHidden|orientation"
25、0; > <!- This filter says that we can view or edit the data of
26、 a single note -> <intent-filter android:label="string/resolve_edit">
27、160; <action android:name="ent.action.VIEW" /> <action android:name="ent.action.EDIT" />
28、160; <action android:name="com.android.notepad.action.EDIT_NOTE" /> <category android:name="ent.catego
29、ry.DEFAULT" /> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </intent-
30、filter> <!- This filter says that we can create a new note inside o
31、f a directory of notes. -> <intent-filter> <action android:name="ent.action.
32、INSERT" /> <category android:name="ent.category.DEFAULT" />
33、<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> </intent-filter> </activity>上面第一個intent-filter中有一個action 名為,而前面我們創(chuàng)建的Intent也正好
34、是Intent.ACTION_EDIT=” ent.action.EDIT”,想必大家已經(jīng)明白是怎么回事了吧。下面就進入activity選擇機制了:系統(tǒng)從intent中獲取道uri,得到了content:/vider.NotePad/notes/1,去掉開始的content:標識,得到vider.NotePad/notes/1,然后獲取前面的,然后就到Androidmanfest.xml中找到authorities為的provider,這個就是后面要講的contentprovider,然后就加載這個content p
35、rovider。 <provider android:name="NotePadProvider" android:authorities="vider.NotePad" />在這
36、里是NotePadProvider,然后調(diào)用NotePadProvider的gettype函數(shù),并把上述URI傳給這個函數(shù),函數(shù)返回URI所對應的類型(這里返回Notes.CONTENT_ITEM_TYPE,代表一條日志記錄,而CONTENT_ITEM_TYPE = " vnd.android.cursor.item/vnd.google.note ")。 Override public String getType(Uri uri)
37、0; switch (sUriMatcher.match(uri) case NOTES: return Notes.CONTENT_TYPE; case
38、160;NOTE_ID: return Notes.CONTENT_ITEM_TYPE; default: throw new IllegalArgumentException("U
39、nknown URI " + uri); 上面的sUriMatcher.match是用來檢測uri是否能夠被處理,而sUriMatcher.match(uri)返回值其實是由決定的。 sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH)
40、; sUriMatcher.addURI(NotePad.AUTHORITY, "notes", NOTES); sUriMatcher.addURI(NotePad.AUTHORITY, "notes/#", NOTE_ID);然后系統(tǒng)使用獲得的" vnd.android.cursor.item/vnd.google.note "和”ent.action.EDIT”到androidmanfest.xml中去找匹配的activity. <intent-filter android:label="string/resolve_edit"&g
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- GGFG-PAB-Exatecan-TFA-生命科學試劑-MCE-7805
- 2025年度私教健身中心教練團隊合作協(xié)議
- 2025年度水產(chǎn)養(yǎng)殖技術支持租地合同
- 二零二五年度私人酒店健身教練用工協(xié)議
- 材料采購合同
- 食堂操作間衛(wèi)生與消毒措施
- 河道清淤施工方案6篇
- 個人不服勞動合同糾紛仲裁起訴狀范本
- 上海簡易離婚合同模板
- 上海市商品住宅銷售合同模板
- 長沙醫(yī)學院《無機化學》2021-2022學年第一學期期末試卷
- eras婦科腫瘤圍手術期管理指南解讀
- GB/T 750-2024水泥壓蒸安定性試驗方法
- 初一到初三英語單詞表2182個帶音標打印版
- 《人力資源管理》全套教學課件
- 【課件】2024-2025學年高一上學期英語開學第一課課件
- 年度重點工作計劃
- 《經(jīng)濟思想史》全套教學課件
- 環(huán)境衛(wèi)生學及消毒滅菌效果監(jiān)測
- 對合同條款有異議函
- 中醫(yī)館工作細則
評論
0/150
提交評論