Android實(shí)例剖析筆記(最終版)_第1頁
Android實(shí)例剖析筆記(最終版)_第2頁
Android實(shí)例剖析筆記(最終版)_第3頁
Android實(shí)例剖析筆記(最終版)_第4頁
Android實(shí)例剖析筆記(最終版)_第5頁
已閱讀5頁,還剩88頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、enbin1#發(fā)短消息打印加為好友蘭體大小:ttenbin (enbin)當(dāng)前離二發(fā)表于2010-6-24 18:241只看該作者經(jīng)驗(yàn)分享android實(shí)例剖析筆記系列文章本帖最后由enbin于2010-6-24 19:01編輯線uid42308帖子503精華6積分3919學(xué)分688閱讀權(quán)限100來自廣東廣州市 在線時(shí)間41小時(shí) 注冊(cè)時(shí)間 2010-3-28今天在博客園里逛的時(shí)候看到一個(gè)博客連載的android實(shí)例學(xué)習(xí)筆記, 讀過之后獲益匪淺,在此分亨給人家,希望對(duì)大家也有幫助。二樓:android實(shí)例剖析筆記三樓:android實(shí)例剖析筆記四樓:五樓:七樓:7、樓優(yōu)秀版主uid42308積分

2、3919學(xué)分688(一)(二)android實(shí)例剖析筆記android實(shí)例剖析筆記android實(shí)例剖析筆記android實(shí)例剖析筆記八樓:android實(shí)例剖析筆記九樓:作者:出處:學(xué)分如何兌換禮品1來自廣東-廣州市(三)(四)(五)(六)(七)android實(shí)例剖析筆記(八)洞庭散人評(píng)分次數(shù)shengyong線u1d42308 帖子503 精華6積分3919 學(xué)分688閱讀權(quán)限100來自 廣東廣州市 在線時(shí)間41小時(shí)注冊(cè)時(shí)間優(yōu)秀版主樓主辛苦了,感謝分厚積分+ 10學(xué)分+5學(xué)分是什么本主題由devil于2010-7-16 10:01解除置頂收藏評(píng)分回復(fù)引用訂閱top分享到:會(huì)型除令qe3處出

3、區(qū)genbin發(fā)短消息加為好友enbin (enbin)iij前離2#發(fā)表于2010-6-24 18:28 1只看該作者開港語俗話說,“熟讀唐詩三冇首,不會(huì)作詩也會(huì)吟二最近收集了很多android 的示例代碼,從這些代碼的閱讀和實(shí)驗(yàn)中學(xué)習(xí)到很多知識(shí),從陽產(chǎn)生寫 這個(gè)系列的打算,目標(biāo)就是一步步跟著實(shí)例進(jìn)行動(dòng)手實(shí)作,真正從“做” 中體會(huì)和學(xué)習(xí)android開發(fā)。本文是這個(gè)系列的第一篇,fi標(biāo)是android自帶的一個(gè)范例程序: 記事本,將分為四篇文章進(jìn)行詳細(xì)介紹。預(yù)備知識(shí)搭建開發(fā)環(huán)境,嘗試編寫"hello world", 了解android的基木概念,熟 悉android的ap

4、i(官方文檔屮都有,不贅述)。程序截圖先來簡(jiǎn)單了解下程序運(yùn)行的效來uid42308 積分3919學(xué)分688學(xué)分如何兌換禮品來口廣東廣州市1:19 pmedit note第二猜亠寫點(diǎn)啥好呢起來很美,不知道真正摸起來啥 感覺哈?revertiiideleteedit title1:19 pm iidla 1:20 pm第一篇,寫點(diǎn)啥好呢?deleteadd note啊,又要寫一篇么?q第一篇,寫點(diǎn)啥好呢?下載(23.28 kb)2010-6-24 18:27程序入口點(diǎn)類似于win32程序里的winmain函數(shù),android自然也有它的程序入口 點(diǎn)。它通過在androidmanifest.xml文

5、件中配置來指明,可以看到名為 noteslist 的 activity 節(jié)點(diǎn)下有這樣一個(gè) intent-filter,其 action 為 ent.action.main,categoiy 扌h 定為 ent.category.launcher,這就指明了 這個(gè) activity是作為入口 activity,系統(tǒng)查找到它后,就會(huì)創(chuàng)建這個(gè)activity實(shí) 例來運(yùn)行,若耒發(fā)現(xiàn)就不啟動(dòng)(你可以把main改名字試試)。<intent-filter><action android:name="ent.acti

6、on.main"/><category android:name="ent.category.launcher"/></intent-filter>noteslist 詳解就從入口點(diǎn)所在的activity(見圖1)開始,町以看到這個(gè)activity ms要的 功能就是顯示h志列表。這個(gè)程序的h志都存放在sqlite數(shù)據(jù)庫屮,因 此需要讀取出所有的h志記錄并顯示。先來看兩個(gè)重婆的私有數(shù)據(jù),第一個(gè)projection字段指明了“日志列 表“所關(guān)注的數(shù)據(jù)庫中的字段(即只需要id和title就可以了)。privatest

7、aticfinal string projection =new string notesd, / 0notes.title, / 1;笫二個(gè)字段column_index_title ?旨明title字段在數(shù)據(jù)表屮的索弓i。 privatestatic finalint columnndex_title =1;然后就進(jìn)入第一個(gè)調(diào)用的函數(shù)oncreateointent intent = getlntent();訐(intcnt.gctdata()=null) intent.setdata(notes.content_url);因?yàn)閚oteslist這個(gè)activity是系統(tǒng)調(diào)用的,此時(shí)的inte

8、nt是不帯數(shù) 據(jù)和操作類型的,系統(tǒng)只是在其中指明了冃標(biāo)組件是notelist,所以這 里把content:/ vidcr.notcpad/notcs保存至!j intent 里面,這 個(gè)uri地址指明了數(shù)據(jù)庫屮的數(shù)據(jù)表名(參見以后的notepadprovider 類),也就是保存日志的數(shù)據(jù)表notesocursor cursor = managedquery(getintent().getdata(), projection, null, null, notes.default_sort_order);然后調(diào)用managcdqucry函數(shù)查詢出所冇的li志信息,這

9、里第一個(gè) 參數(shù)就是上面設(shè)置的 content:/ vider.notepad/notes?這個(gè) uri,即notes數(shù)據(jù)表。projection字段指明了結(jié)果中所需要的字段, notes.default_sort_order指明了結(jié)果的排序規(guī)則。實(shí)際上 managedquery并沒有宜接去杳詢數(shù)據(jù)庫,而是通過content provider來 完成實(shí)際的數(shù)據(jù)庫操作,這樣就實(shí)現(xiàn)了邏輯層和數(shù)據(jù)庫層的分離。 simplccursoradaptcr adapter = new simplecursoradapter(this, rayout.noteslist_item,

10、cursor,new stringj notes.title , newintj android.r.id.texti );setlistadapter(adapter);查詢出口志列表后,構(gòu)造一個(gè)cursoradapter,并將其作為list view 的數(shù)據(jù)源,從而在界面上顯示出h志列表??梢钥吹?,第二個(gè)參數(shù)是 r.layout.noteslist_itemi 打開對(duì)丿.'v的 noteslist_item.xml 文件, <textview xmlns:android=h android:id=nandroid:id/textluandroid: layout_width=

11、mfill_parentm android:layout_height=n?android:attr/listpreferreditemheightn android:textappearance=m?android:attr/textappearancelargem android:gravity=,fcenter_verticalmandroid:paddinglcft=h5dip"android: singleline=htruem/>就是用來顯示一條u志記錄的tcxtvicw,最后兩個(gè)字段指明了實(shí)際 的字段映射關(guān)系,通過這個(gè)textview來顯示一條日志記錄的title

12、字段。處理“選擇日志”事件既然冇了“口志列表”,就自然要考慮如何處理某一條口志的單擊事件, 這通過重載onlistltemclick方法來完成, overrideprotectedvoid onlistitemclick(listview 1, view v, int position, long id) uri uri = contenturis.withappendedid(getintent().getdata(), id);string action = getintent().getaction();if (intent. action_pick.equals(action)iiin

13、tent.action_get_content.equals(action) / the caller is waiting for us to return a note selected by / the use匚 the have clicked on one, so return it now. setresult(result_ok, new intent().setdata(uri); else / launch activity to view/edit the currently selected item startactivity(ncw intcnt(intcnt.act

14、ion_edit, uri);首先通過"content:/ com.googivideenotepad/notesm和日志的 id 號(hào)拼接得到選中日志的真正uri,然后創(chuàng)建一個(gè)新的intent,其操作類型為 lntent.action_ed!t,數(shù)據(jù)域指出待編輯的口志ur1 (這里只分析else 塊)。intent深度剖析那么,上面這句 startactivity(new intent(intent.action_edit, uri)執(zhí)行 后會(huì)發(fā)牛什么事情呢?這時(shí)候android系統(tǒng)就跳出來接管了,它會(huì)根據(jù) intent屮的信息找至i對(duì)應(yīng)的activity,在這里找到的是n

15、oteeditor這個(gè) activity,然后創(chuàng)建這個(gè)activity的實(shí)例并運(yùn)行。那么,android 乂是如何找到notceditor這個(gè)對(duì)應(yīng)的activity的呢?這就 是intent發(fā)揮作用的時(shí)刻了。new intent(intent.action_edit, uri)這里的 intent.action_edit=” ent.action.edit,另外通過設(shè) 置斷點(diǎn),我們看下這里的uri值:0 -?r rurir= urishierarchicaluri (id =830059050968)日 et authority = urispart (id =8 300

16、 58677160)田 decoded = rcom. google. provider. notepadr 田 encoded = rcom. google. provider.notepadr由 et fragment= urispartsemptypart (id =830003817304) 田曰 host= wtcached"et path= urispathpart (id =8 300 590 50944)田 decoded = *not cachedr田 encoded = 7notes/r卜載(24.92 kb)2010-6-24 18:27可以看到選中的口志條1=

17、1的uri是: content:/vider.notepad/notes/l然后我們?cè)賮砜床穉ndroidmanfest.xml,其中有這個(gè)provider<provider android:name=hnotepadproviderhandroid:authorities=hvide 匚 n otepact/>發(fā)現(xiàn)沒有?它也有vider.notepad ,這個(gè)是 contcnt:/vidcr.notcpad/notcs/l 的一部分,同時(shí)<activity and

18、roid:name=hnoteeditorm android:theme=handroid:style/theme.light0 android:label=nstring/title_notem android:screenorientation=nsensorn android:configchangcs=mkcyboardhiddcnloricntationh><! this filter says that we can view or edit the data ofa single note -><intent-filter android:label=,f

19、 string/resolve_edit ,r><action android:name=ent.action.view,f/> <action android:name=ent.action.editm/><action android:name=,fcom.android.notepad.action.edit_notem/><category android:name=hent.category.defaulth/><data android:mimetype

20、=mvnd.android.cursor.item/vnd.google.notem/></intent-filter><! this filter says that we can create a new note insideof a directory of notes.-><intent-filter><action android:name=ent.action.insertn/><category android:name=hent.category.defaulth/>

21、;<data android:mimetype=hvnd.android.cursor.dir/vnd.google.note”/> </intent-filter></activity>上而第一個(gè)intent-filter中有一個(gè) action 名為 ent.action.edit,而前面我們創(chuàng)建的intent也正好是intent.action_edit=, ent.action.edit' 想必人家已經(jīng)明ft 是怎么回事了吧。下而就進(jìn)入activity選擇機(jī)制了:系統(tǒng)從 intent 屮獲取道 uri

22、, 得到 了 content:/com.goog)vider.notepad/notes/l,去掉開始的 content:標(biāo)識(shí), 得至u vider.notepad/notes/1,然后獲取前面的 vider.notepad ,然后就到 androidmanfest.xml 中找到 authorities 為 vider.notepad 的 provider,這個(gè)就是后面耍講的 contentprovider,然后就加載這個(gè) content provider<provider android:n

23、anie=unotepadprovider"android:authorities="vide r.notepad"/>在這里是 notepadprovider,然后調(diào)用 notepadprovider 的 gettype 函數(shù),并 把上述uri傳給這個(gè)函數(shù),函數(shù)返回uri所對(duì)應(yīng)的類型(這里返回 notes.content_item_type ,代表一條日志記錄,而 content_item_type = " vnd.android.cursor.item/vnd.google.note ")。 overrid

24、epublic string gcttypc(uri uri) switch (surimatcher.match(uri) case notes:return notes.content_type;case note_id:return notcs.content_item_type;default:throw+ uri);上而的surimatcher.match是用來檢測(cè)uri是否能夠被處理,而 surimatcher.match(uri)返回值其實(shí)是由surimatchcr =new urimatcher(urimatcher.no_match); surimatcher.adduri(

25、notepad.authority, “notes”,notes); surimatcher.adduri(notepad.authority, “notes/#", note.1d);決定的。然后系統(tǒng)使川獲得的"vnd.android.cursor.item/vnd.google.note ” 和''ent.action.edit” 到 androidmanfest.xml 中去找匹配的 activity <intent-filter android:labcl=nstring/rcsolvc_cditn><acti

26、on android:name=ent.action.viewu/><action android:name=hent.action.editm/><action android:namc=ucom.android.notepad.action.edit_notem/><category android:name=ent.category.defaulth/><data android:mimctypc=hvnd.android.cursor.itcm/vnd.google.n

27、ote"/></intent-filter>正好notceditor這個(gè)activity的intent-filter滿足上述條件,這樣就找到 noteeditoro j*是系統(tǒng)加載這個(gè)類并實(shí)例化,運(yùn)行,然后就到了 noteeditor 的0ncreate函數(shù)中(見后續(xù)文章)。小技巧1, 在命令行屮使用nadb shelf,命令進(jìn)入系統(tǒng)屮,然后”cd app"進(jìn)入應(yīng)用 程序所在冃錄,smxxx"就nj以刪除你指定的apk,從而去掉其在系統(tǒng) 頂層界面占據(jù)的圖標(biāo),若兩次”cd data”則可以進(jìn)入應(yīng)丿ij程序使川的數(shù)據(jù) 目錄,你的數(shù)據(jù)可以保存在這里,例

28、如notepad就是把其數(shù)據(jù)庫放在它 的 databases fl 錄下,名為 notc_pad.db.2, 第一次啟動(dòng)模擬器會(huì)比較慢,但以后就別關(guān)閉模擬器了,修改代碼, 調(diào)試都不需要再次啟動(dòng)的,直接修改后run或debug就是?;貜?fù)引用topenbin3#發(fā)短消息d發(fā)表于2010-6-24 18:321只看該作者加為好友上篇文章分析了 noieslist這個(gè)activity,并著重剖析了其中的inlent機(jī)制,木enbin (enbin)文將繼續(xù)上篇未完的工作,以noteslist為實(shí)例介紹android的菜單機(jī)制(尤當(dāng)前離線 其是動(dòng)態(tài)菜單機(jī)制)。uid簡(jiǎn)介42308android 提供了三

29、種菜單類型,分別為 options menu,context menu,submenuo帖了options menu就是通過按home鍵來顯示,context menu需要在view上按上2s503后顯示。這兩種menu 冇可以加入子菜單,子菜單不能種不能嵌套了菜單。精華options menu最多只能在屏幕最下面顯示6個(gè)菜單選項(xiàng),稱為icon menu, icon6menu不能冇checkable選項(xiàng)。多于6的菜單項(xiàng)會(huì)以more icon menu來調(diào)出,積分稱為 expanded menu。options menu 通過 activity 的 oncreateoptionsmenu 來生3

30、919成,這個(gè)函數(shù)只會(huì)在menu第一次生成吋調(diào)用。任何想改變options menu的想學(xué)分法只能在onprcparcoptionsmcnu來實(shí)現(xiàn),這個(gè)函數(shù)會(huì)在menu顯示前調(diào)用。688onoptionsitemselected用來處理選屮的菜單項(xiàng)。閱讀權(quán)限context menu是跟某個(gè)具體的view綁定在一起,在activity種用100registerforcontextmenu 為某個(gè) view 注冊(cè) context menu。context menu 在顯來 fl示前都會(huì)調(diào)用 oncreatecontextmenu 來生成 menuo oncontextltemselected 用來

31、 廣東-廣州市處理選中的菜單項(xiàng)。在線時(shí)間android還提供了對(duì)菜單項(xiàng)進(jìn)行分組的功能,可以把相似功能的菜單項(xiàng)分成同41小時(shí) 一個(gè)組,這樣就可以通過調(diào)用 setgroupcheckable , 注冊(cè)時(shí)間setgroupenabled,setgroupvisible來設(shè)置菜單屬性,而無須單獨(dú)設(shè)置。2010-3-28 options menu優(yōu)秀版上notepad屮使用了 options menu和context menu兩種菜單°首先來看生成 options menu 的 oncreateoptionsmenu 函數(shù)。menu.add(0, menu_item_insert, 0, r.

32、string.menu_insert) .setshortcutc3 'a').seticon(android.r.drawable.ic_menu_add);uid這是一個(gè)標(biāo)準(zhǔn)的插入一個(gè)菜單項(xiàng)的方法,菜單項(xiàng)的id為 menu_item_insert。42308冇意思的是下面這幾句代碼: 積分3919intent intent =688學(xué)分如何兌換禮品來自廣東-廣州市intcnt.addcatcgory(intcnt.category_alternative); menu.addintentoptions(menu.category_alternative, 0, 0,new

33、componentname(this, noteslist.class), null, intent, 0, null);學(xué)分new intent(null, getintent().getdata();這到底有何用處呢?其實(shí)這是-種動(dòng)態(tài)菜單技術(shù)(也有點(diǎn)像插件機(jī)制),若某一 個(gè) activity , 其類型是 ent.category.alternative , 數(shù)據(jù) 是"vnd.android.cursor.dir/vnd.google.no©'的話,系統(tǒng)就會(huì)為這個(gè) activity 增加 一個(gè)菜單項(xiàng)。在androidmanfest.xml屮

34、杏看后發(fā)現(xiàn),沒有一個(gè)activity符合條 件,所以這段代碼并沒有動(dòng)態(tài)添加出任何一個(gè)菜單項(xiàng)。為了驗(yàn)證上述分析,我們可以來做-個(gè)實(shí)驗(yàn),在androidmanfest.xml中進(jìn)彳亍修 改,看是否會(huì)動(dòng)態(tài)牛成出菜單項(xiàng)。實(shí)驗(yàn)一首先我們來創(chuàng)建一個(gè)新的activity作為fl標(biāo)activity,名為helloandwid,沒有什 么功能,就是顯示一個(gè)界面。publicclass helloandroid extends activity (overrideprotectedvoid oncreate(bundle savedlnstancestate) supe 匚 o ncreate(savedlnst

35、ancestate); this.setcontentview(r.layout.main);它所對(duì)應(yīng)的布局界而xml文件如門<?xml vcrsion=n1.0h encoding=nutf-8m?><linearlayout xmlns:android=h android:orientation=hverticar,android:layout_width=hfill_parentm android:layout_height=hfill_parentn><textviewandroid:layout_width=mfill_parenth android:l

36、ayout_height=nwrap_contenth android:id=h+id/textviewo 17> <buttonandroid: id=u +id/b u ttono 1handroid: lay ou t_height=n wrap.contentmandroid:layout_width=nfih_parcntn android:tcxt=mstring/txtinfoh></button> </linearlayout>然后修改androidmanfest.xml,加入卜而這段配置,讓helloandroid滿足上述 兩個(gè)條件:

37、<activity android:name=hhelloandroidu android:label=mstring/txtinfoh> <intent-filter><action android:name=,fcom.android.notepad.action.hello_testm/><category android:name=hent.category.alternative,7><data android:mimetype=nvnd.android.cursor.dir/vnd.google.noteh

38、/></intent-filter></activity>好了,運(yùn)行下試試,哎,還是沒有動(dòng)態(tài)菜單項(xiàng)加入呀!怎么冋事呢?杏看代碼示發(fā)現(xiàn),原來是onprepareoptionsmenu搞的鬼!這個(gè) 函數(shù)在oncrcatcoptionsmcnu z后運(yùn)行,下面這段代碼中,由于 menu.category_alternative 是指向同一個(gè)組, 所以把 oncreateoptionsmenu中設(shè)置的菜單項(xiàng)給覆蓋掉了,而由于 onprepareoptionsmenu 沒有給 menu.category_alternative 附新值,故 menu.category_alt

39、ernative 還是為空。intent intent =new intent(null, uri);intent.addcategory(l ntent.category_alternative);menu.addintentoptions(menu.category_alternative, 0, 0, null, specifics, intent, 0,items);好的,那我們暫時(shí)把上面這幾句給注釋掉,當(dāng)然,也可以不注釋這幾句,在 oncreateoptionsmenu 中改 groupid 號(hào),即將 menu.category_alternative 改為menu.first,其他

40、的也彳亍,但注意不耍改為menu.none,這樣會(huì)覆蓋掉mcnu.add(o, menu_itemn sert, 0, r. s tri ng. men u_i n sort) .setshortcut(3 'a').setlcon(android.r.drawable.ic_menu_add);添加的菜單。因?yàn)閙cnu.nonc也為0 運(yùn)行后就可以看到動(dòng)態(tài)菜單出來了!下載(23.8 kb)2010-6-24 18:30上而這個(gè)options menu是在noteslist界而上沒有fi志列表選中的情況下牛成 的,若先選中一個(gè)日志,然后再點(diǎn)”menu”,則生成的options

41、menu是下面這 樣的:下載(29.13 kb)2010-6-24 18:3()哎,又動(dòng)態(tài)增加了兩個(gè)菜單項(xiàng)”edit note"和”edit title",這又是如何動(dòng)態(tài)加入的 呢?這就是 onprepareoptionsmenu 的功勞了。uri uri = contenturis.withappendedid(getintent().getdata(), gctsclcctcditcmid();首先獲取選中的ll志(若沒冇選擇,則uri為空)intentj specifics =new intentfll;spccificso=new intent(intent.act

42、ion_edit, uri);menuitemj items =new menulteml;然后為選中的日,忐創(chuàng)建一個(gè)intent,操作類型為intent.action_edit,數(shù)據(jù)為 選中日志的uri.于是會(huì)為選中的口志創(chuàng)建一個(gè)”edit note”菜單項(xiàng)。intent intent =new intcnt(null, uri);intent.addcategory(intent.category_alternative);menu.addintentoptions(menu.category_alternatlve, 0, 0, null, specifics, intent, 0,it

43、ems);這兒句和上llll oncreateoptionsmenu函數(shù)中類似,用于動(dòng)態(tài)增加菜單項(xiàng),若某 一個(gè) activity ,其類型是"ent.category.alternative",數(shù)據(jù) 是,vnd.android.cursor.item/vnd.google.notem的話,系統(tǒng)就會(huì)為這個(gè) activity 增加 一個(gè)菜單項(xiàng)。在androidmanfest.xml屮杳看后發(fā)現(xiàn),titleeditor這個(gè)activity符 合條件,丁是系統(tǒng)就為titleeditoi這個(gè)activity動(dòng)態(tài)添加一個(gè)菜單項(xiàng),'edit title&qu

44、ot;。else menu.removegroup(menu.category_alternative);若口志列表為空,則從菜單中刪除組號(hào)為menu.category.alternative 的菜單項(xiàng),只剩卜fadd note”菜單項(xiàng)。處理“選屮菜單項(xiàng)”事件菜單項(xiàng)選中事件的處理非常簡(jiǎn)單,通過onoptionsitemselected來完成,這里只 是簡(jiǎn)單地調(diào)用 startactivity(new intent(intent.actionnsert, getintent().getdata();這個(gè) intent 的操作類型為 intent.actionjnsert,數(shù)據(jù) 為 i i 志列表的

45、 url 即"content:/ vider.notepad/notes overridepublicboolean onoptionsitemselected(menuitem item) switch (item.getltemldo) case menu_itemnsert:/ launch activity to insert a new itemstartactivity(newintent(intent. actionnsert,getintent().getdata();returntrue;1returnsuper.onoptionslte

46、mselected(item);context menu下面介紹期一種菜單-上下文菜單,這通過重oncrcatccontcxtmcnu函數(shù)實(shí) 現(xiàn)。首先確認(rèn)已經(jīng)選中了 h志列表中的一個(gè)日志,若沒選擇,則直接返回。cursor 指向選中的日志項(xiàng)。cursor cursor = (cursor) gctlistadaptcr().gctitcm(info.position);if (cursor =null) / for some reason the requested item isn't available, do nothingreturn;然后,設(shè)置上下文菜單的標(biāo)題為口志標(biāo)題/ s

47、etup the menu headermenu.setheadertitle(cursor.getstring(columnndex_title);最后為上下文菜單增加一個(gè)菜單項(xiàng)/ add a menu item to delete the notemenu.add© menu_1tem_delete, 0, r.string.menu_delete);對(duì)于上下文菜單項(xiàng)選中的事件處理,是通過重載oncontextltemselected實(shí)現(xiàn) 的。switch (item.getltemldo) case menu_1tem_delete: / delete the note tha

48、t the context menu is forinfo.id);uri noteuri = contenturis.withappendedid(getintent().getdata(),getcontentresolver().delete(noteuri5 null, null);returntrue;returnfalse;對(duì)于日志的刪除,首先調(diào)用 contenturis.withappendedid(getintent().getdata(), info.id);來拼接出待刪除日志的 url然后 getcontentresolver().delete(noteuri, null,

49、 null);調(diào)用卜層的content provider去刪除此fi志。實(shí)驗(yàn)二來做個(gè)簡(jiǎn)單實(shí)驗(yàn),在上述代碼基礎(chǔ)上增加一個(gè)上下文菜單項(xiàng)。首先在 oncrcatccontcxtmcnu函數(shù)中增加一個(gè)上下文菜單項(xiàng):menu.add(0,menutemnsert,orstringmenu_insert);然后為英在oncontcxtltcmsclcctcd函數(shù)中增加一個(gè)處理過程:case menu_item_insert:new alertdialog.builder( this).seticon(r.drawable.app_notes).settitle(r.string.app_name).set

50、message(r.string.error_message).set positivcbutton(r.string.button_ok, new onclicklistcncr()publicvoid onclick(dialoginterface dialog, int which) / todo auto-generated method stub).show();returntrue;實(shí)驗(yàn)結(jié)果如下:衛(wèi) 1:20 pm |lanote pad第一篇,寫點(diǎn)啥好呢?啊,又要寫一篇么?q第一篇,寫點(diǎn)啥好呢?下載(23.28kb)2010-6-24 18:3()ad晉 rd 題 1:20pm第

51、一篇,寫點(diǎn)啥好呢?啊,又要寫一篇么?note paderror loading noteok下載(22.84 kb)2010-6-24 18:3()附記感謝evan jiang對(duì)前一篇文章的錯(cuò)謀之處進(jìn)行指正,'faction android:name=ent.action.mainn /><catcgory android:namc=hcnt.catcgory.launcherh />只是指明會(huì)在launcher中顯示圖標(biāo),同一個(gè)apk可以在桌面上加很多的圖標(biāo), 分別啟動(dòng)內(nèi)部不同的多個(gè)界面?!埃瑢?shí)驗(yàn)后發(fā)現(xiàn)確實(shí)如此,學(xué)習(xí)了。

52、回復(fù)引用topenbin發(fā)短消息加為好友4#發(fā)表于2010-6-24 18:37 1只看該作者上一篇文帝介紹了 android的菜單機(jī)制,并動(dòng)手做了一個(gè)實(shí)驗(yàn)來探究動(dòng)線u1d簡(jiǎn)單的實(shí)驗(yàn)來摸索狀態(tài)轉(zhuǎn)換的機(jī)制,最后介紹notepad中使用的自定義 控件技術(shù)。enbin (enbin)當(dāng)前離態(tài)菜單的實(shí)驗(yàn)機(jī)制。這一篇將重點(diǎn)介紹activity的生命周期,通過一個(gè)實(shí)驗(yàn)一優(yōu)秀版主©©uid42308積分3919學(xué)分688學(xué)分如何兌換禮品來自廣東-廣州市android:icon=hdrawable/iconn42308帖子activity的生命周期503精華activity類屮有許多onx

53、xx形式的函數(shù)可以重載,比如6oncreate,onstart,onstop,onpause,那么它們的調(diào)用順序到底是如何的呢?積分下面就通過一個(gè)實(shí)驗(yàn)來進(jìn)行分析。在做這個(gè)實(shí)驗(yàn)z前,我們先得知道如3919何在android中進(jìn)行l(wèi)og輸出的。學(xué)分我們要使用的是android.util.log類,這個(gè)類相當(dāng)?shù)暮?jiǎn)單易用,因?yàn)樗?88供的全是一些靜態(tài)方法:閱讀權(quán)限100log.v(string tag, string msg);/verbose來自log.d(string tag, string msg);/debug廣東廣州市l(wèi)og.i(string tag, string msg);/info在

54、線時(shí)間log.w(string tag, string msg);/warn41小時(shí)log.e(string tag, string msg);/error注冊(cè)時(shí)間2010-3-28矍1前面的lag是由我們定義的一個(gè)標(biāo)識(shí),一般可以用“類名一方法名“來定義。要在eclipse屮杏看輸出的log信息,需要打開logcat (windowashow viewaotheraandroidalogcat 即可打開)我們要做的實(shí)驗(yàn)非常簡(jiǎn)單,就是有兩個(gè)activity (我這里分別叫做 frmlogin和hello2)它們各自有一個(gè)button, nj以從第一個(gè)跳到第二個(gè), 也可以從第二個(gè)跳回到第一個(gè)。配置文件androidmanifest.xml非常簡(jiǎn)單,第二個(gè)activity并沒有多余的 信息需要指定。<application android: label=n stri ng/app_namem > <activity android:namc=,'.frmlogin"android:label="string/app_name"><intent-filter><action android:name="ent.action

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論