安卓Android Context類實(shí)例詳解
1.例如下面的代碼片段,Toast類的第一個(gè)參數(shù)接受一個(gè)Context對(duì)象:
@Override protected Dialog onCreateDialog(int id) { switch (id) { case 0: Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("This is a dialog with some simple text..."); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show(); } }); builder.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText( getBaseContext(), items[which] + (isChecked ? " checked!" : " unchecked!"), Toast.LENGTH_SHORT).show(); } }); return builder.create(); } return null; } @Override protected Dialog onCreateDialog(int id) { switch (id) { case 0: Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("This is a dialog with some simple text..."); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show(); } }); builder.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText( getBaseContext(), items[which] + (isChecked ? " checked!" : " unchecked!"), Toast.LENGTH_SHORT).show(); } }); return builder.create(); } return null; }
然而,Toast類并沒有直接用在Activity中,它被用在了AlertDialog類中。所以,這里需要通過getBaseContext()方法獲取一個(gè)Context類的實(shí)例。
2.在Activity中動(dòng)態(tài)地創(chuàng)建一個(gè)視圖的時(shí)候也會(huì)遇見Context。
例如,如果想通過硬編碼動(dòng)態(tài)地創(chuàng)建一個(gè)TextView:
TextView tv = new TextView(this); TextView tv = new TextView(this);
TextView的構(gòu)造器接受一個(gè)Context對(duì)象,因?yàn)锳ctivity類是Context類的子類,所以可以用this關(guān)鍵字來代替這個(gè)Conext對(duì)象。
提示:
使用this動(dòng)態(tài)地創(chuàng)建視圖,例如TextView、Button,存在一個(gè)潛在的風(fēng)險(xiǎn)——內(nèi)存泄漏。所以,盡可能地使用getApplicationContext()方法替代this。
- Android 中Context的使用方法詳解
- Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對(duì)象的方法詳解
- Android全局獲取Context實(shí)例詳解
- Android編程實(shí)現(xiàn)為ListView創(chuàng)建上下文菜單(ContextMenu)的方法
- Android context源碼詳解及深入分析
- Android面試筆記之常問的Context
- 談?wù)凙ndroid里的Context的使用實(shí)例
- 避免 Android中Context引起的內(nèi)存泄露
- 詳解Android中的Context抽象類
- 深入解析Android App開發(fā)中Context的用法
- Android編程獲取全局Context的方法
- Android編程中context及全局變量實(shí)例詳解
- Android中ContextMenu用法實(shí)例
- android基礎(chǔ)教程之context使用詳解
- Android獲取其他包的Context實(shí)例代碼
- android中Context深入詳解
相關(guān)文章
Android仿微信頁面底部導(dǎo)航效果代碼實(shí)現(xiàn)
本文給大家分享一段代碼有關(guān)android仿微信頁面底部導(dǎo)航效果代碼實(shí)現(xiàn)的思路,非常不錯(cuò),感興趣的朋友一起看看吧2016-09-09詳解Android(共享元素)轉(zhuǎn)場動(dòng)畫開發(fā)實(shí)踐
本篇文章主要介紹了詳解Android(共享元素)轉(zhuǎn)場動(dòng)畫開發(fā)實(shí)踐,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-08-08Android仿微信右上角點(diǎn)擊加號(hào)彈出PopupWindow
這篇文章主要為大家詳細(xì)介紹了Android仿微信右上角點(diǎn)擊加號(hào)彈出PopupWindow,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Android在WebView中調(diào)用系統(tǒng)下載的方法
這篇文章主要為大家詳細(xì)介紹了Android在WebView中調(diào)用系統(tǒng)下載的簡單使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android開發(fā)實(shí)現(xiàn)可拖動(dòng)排序的ListView功能【附源碼下載】
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)可拖動(dòng)排序的ListView功能,結(jié)合實(shí)例形式分析了Android列表拖動(dòng)排序原理與相關(guān)操作技巧,并附帶完整源碼供讀者下載參考,需要的朋友可以參考下2017-11-11ViewFlipper實(shí)現(xiàn)上下翻滾輪播效果
這篇文章主要為大家詳細(xì)介紹了ViewFlipper實(shí)現(xiàn)上下翻滾輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08