精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
談談Android里的Context的使用!!!

大家好,今天給大家分享一下Android里的Context的一些用法,以前經常有人在群里問我比如我在一個工具類里的某個方法,或者View里需要調用Context.但是工具類還有View里沒有這個上下文怎么辦?為了解決大家的疑問,為了解決大家的疑問,我今天寫一個簡單的Demo.讓大家如何學好自如的用Context.想什么時候有Context,什么時候就有Context.

這里大致可以分為兩種:一是傳遞Context參數,二是調用全局的Context.

其實我們應用啟動的時候會啟動Application這個類,這個類是在AndroidManifest.xml文件里其實是默認的

  1. <application  
  2.        android:icon="@drawable/ic_launcher"  
  3.        android:label="@string/app_name"  
  4.        >  
  5.        <activity  
  6.            android:name=".ApplicationDemoActivity"  
  7.            android:label="@string/app_name" >  
  8.            <intent-filter>  
  9.                <action android:name="android.intent.action.MAIN" />  
  10.                <category android:name="android.intent.category.LAUNCHER" />  
  11.            </intent-filter>  
  12.        </activity>  
  13.    </application>  

這個Application類是單例的,也就是說我們可以自己寫個Application(比如名為:MainApplication)類,來代替默認的Applicaiton,這個類可以保存應用的全局變量,我們可以定義一個全局的Context.供外部調用.用法如下:

  1. package com.tutor.application;  
  2.   
  3. import android.app.Application;  
  4. import android.content.Context;  
  5.   
  6. public class MainApplication extends Application {  
  7.   
  8.     /** 
  9.      * 全局的上下文. 
  10.      */  
  11.     private static Context mContext;  
  12.       
  13.     @Override  
  14.     public void onCreate() {  
  15.         super.onCreate();  
  16.           
  17.         mContext = getApplicationContext();  
  18.           
  19.     }     
  20.       
  21.     /**獲取Context. 
  22.      * @return 
  23.      */  
  24.     public static Context getContext(){  
  25.         return mContext;  
  26.     }  
  27.       
  28.       
  29.     @Override  
  30.     public void onLowMemory() {  
  31.         super.onLowMemory();  
  32.     }  
  33.       
  34.       
  35. }  

我們需要在AndroidMainifest.xml把MainApplication注冊進去(第10行代碼):

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.tutor.application"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.       
  7.     <application  
  8.         android:icon="@drawable/ic_launcher"  
  9.         android:label="@string/app_name"  
  10.         android:name=".MainApplication" >  
  11.         <activity  
  12.             android:name=".ApplicationDemoActivity"  
  13.             android:label="@string/app_name" >  
  14.             <intent-filter>  
  15.                 <action android:name="android.intent.action.MAIN" />  
  16.                 <category android:name="android.intent.category.LAUNCHER" />  
  17.             </intent-filter>  
  18.         </activity>  
  19.     </application>  
  20.       
  21. </manifest>  

為了讓大家更容易理解,寫了一個簡單的Demo.步驟如下:

第一步:新建一個Android工程ApplicationDemo,目錄結構如下:


第二步:新建MainApplication.java,代碼和上面一樣我就不貼了.

第三步:新建一個工具類ToolsUtil.java,代碼如下

  1. package com.tutor.application;  
  2.   
  3. import android.content.Context;  
  4. import android.widget.Toast;  
  5.   
  6. /** 
  7.  * @author frankiewei. 
  8.  * 應用的一些工具類. 
  9.  */  
  10. public class ToolUtils {  
  11.       
  12.     /** 
  13.      * 參數帶Context. 
  14.      * @param context 
  15.      * @param msg 
  16.      */  
  17.     public static void showToast(Context context,String msg){  
  18.         Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();  
  19.     }  
  20.       
  21.     /** 
  22.      * 調用全局的Context. 
  23.      * @param msg 
  24.      */  
  25.     public static void showToast(String msg){  
  26.         Toast.makeText(MainApplication.getContext(), msg, Toast.LENGTH_SHORT).show();  
  27.     }  
  28. }  

第四步:新建一個View命名為MainView.java就是我們Activity現實的View.代碼如下:

  1. package com.tutor.application;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.util.AttributeSet;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.FrameLayout;  
  10.   
  11. /** 
  12.  * @author frankiewei. 
  13.  * 自定義的MainView. 
  14.  */  
  15. public class MainView extends FrameLayout implements View.OnClickListener{  
  16.       
  17.     private Context mContext;  
  18.       
  19.     private Activity mActivity;  
  20.       
  21.     /** 
  22.      * 參數Button. 
  23.      */  
  24.     private Button mArgButton;  
  25.       
  26.     /** 
  27.      * 全局Button. 
  28.      */  
  29.     private Button mGlobleButton;  
  30.       
  31.     /** 
  32.      * 退出Button. 
  33.      */  
  34.     private Button mExitButton;  
  35.       
  36.     public MainView(Context context){  
  37.         super(context);  
  38.         setupViews();  
  39.     }  
  40.   
  41.     public MainView(Context context, AttributeSet attrs) {  
  42.         super(context, attrs);  
  43.         setupViews();  
  44.     }  
  45.       
  46.       
  47.     private void setupViews(){  
  48.         //獲取View的上下文.  
  49.         mContext = getContext();  
  50.         //這里將Context轉換為Activity.  
  51.         mActivity = (Activity)mContext;  
  52.         LayoutInflater inflater = LayoutInflater.from(mContext);  
  53.         View v = inflater.inflate(R.layout.main, null);  
  54.         addView(v);  
  55.           
  56.         mArgButton = (Button)v.findViewById(R.id.arg_button);  
  57.         mGlobleButton = (Button)v.findViewById(R.id.glo_button);  
  58.         mExitButton = (Button)v.findViewById(R.id.exit_button);  
  59.           
  60.         mArgButton.setOnClickListener(this);  
  61.         mGlobleButton.setOnClickListener(this);  
  62.         mExitButton.setOnClickListener(this);  
  63.     }  
  64.   
  65.     public void onClick(View v) {  
  66.         if(v == mArgButton){  
  67.             ToolUtils.showToast(mContext, "我是通過傳遞Context參數顯示的!");  
  68.         }else if(v == mGlobleButton){  
  69.             ToolUtils.showToast("我是通過全局Context顯示的!");  
  70.         }else{  
  71.             mActivity.finish();  
  72.         }  
  73.     }  
  74.   
  75. }  
這里MainView.java使用的布局main.xml代碼如下:

  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="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Welcome to frankie wei's blog."   
  11.         />  
  12.       
  13.     <Button  
  14.         android:id="@+id/arg_button"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="傳遞Context參數"  
  18.         />  
  19.       
  20.     <Button  
  21.         android:id="@+id/glo_button"  
  22.         android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="全局的Context"  
  25.         />  
  26.       
  27.     <Button  
  28.         android:id="@+id/exit_button"  
  29.         android:layout_width="fill_parent"  
  30.         android:layout_height="wrap_content"  
  31.         android:text="退出App"  
  32.         />  
  33.   
  34. </LinearLayout>  

第五步:修改ApplicationDemoActivity.java,代碼如下:

  1. package com.tutor.application;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class ApplicationDemoActivity extends Activity {  
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.           
  11.         MainView mMainView = new MainView(this);  
  12.         setContentView(mMainView);  
  13.      
  14.     }  
  15.       
  16. }  

第六步:運行上述工程效果如下:

 
  

運行效果1                                                            運行效果2---- 點擊第一個按鈕


 運行效果3---- 點擊第二個按鈕

好了今天就講到這里,大家對Context有什么疑問的,可以留言!!!

源代碼點擊進入==>

本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
LayoutInflater作用及使用
Android中context及全局變量小析
一步一步android(7):關于界面控件的學習【gridview、button、imagebutton】
自己的AppWidget一個簡單教程 - 開發 - Android - JavaEye群組
實現美團、餓了么購物車效果,并本地存儲相關數據
android-BaseAdapter中的notifyDataSetChanged()
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服

主站蜘蛛池模板: 长垣县| 登封市| 衡山县| 天水市| 祁阳县| 奉化市| 五河县| 遂川县| 天气| 大理市| 文登市| 柯坪县| 定南县| 石柱| 南川市| 额尔古纳市| 斗六市| 蕲春县| 玛多县| 枞阳县| 襄城县| 简阳市| 伊宁县| 沛县| 美姑县| 焦作市| 商丘市| 信阳市| 曲阜市| 杂多县| 阿图什市| 锦州市| 东乡县| 桂林市| 资阳市| 新邵县| 丰城市| 灌云县| 绥中县| 卓资县| 孝昌县|