欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

談?wù)凙ndroid里的Context的使用實例

 更新時間:2016年11月19日 16:31:49   作者:Android_Tutor  
這篇文章主要介紹了談?wù)凙ndroid里的Context的使用實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

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

這里大致可以分為兩種:一是傳遞Context參數(shù),二是調(diào)用全局的Context.

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

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    > 
    <activity 
      android:name="ApplicationDemoActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
        <action android:name="androidintentactionMAIN" /> 
        <category android:name="androidintentcategoryLAUNCHER" /> 
      </intent-filter> 
    </activity> 
  </application> 

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

package com.tutor.application; 
 
import androidappApplication; 
import androidcontentContext; 
 
public class MainApplication extends Application { 
 
  /** 
   * 全局的上下文 
   */ 
  private static Context mContext; 
   
  @Override 
  public void onCreate() { 
    superonCreate(); 
     
    mContext = getApplicationContext(); 
     
  }   
   
  /**獲取Context 
   * @return 
   */ 
  public static Context getContext(){ 
    return mContext; 
  } 
   
   
  @Override 
  public void onLowMemory() { 
    superonLowMemory(); 
  } 
   
   
} 

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

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemasandroidcom/apk/res/android" 
  package="comtutorapplication" 
  android:versionCode="1" 
  android:versionName="0" > 
   
  <application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:name="MainApplication" > 
    <activity 
      android:name="ApplicationDemoActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
        <action android:name="androidintentactionMAIN" /> 
        <category android:name="androidintentcategoryLAUNCHER" /> 
      </intent-filter> 
    </activity> 
  </application> 
   
</manifest> 

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

第一步:新建一個Android工程ApplicationDemo,目錄結(jié)構(gòu)如下:

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

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

package com.tutor.application; 
import androidcontentContext; 
import androidwidgetToast; 
 
/** 
 * @author frankiewei 
 * 應(yīng)用的一些工具類 
 */ 
public class ToolUtils { 
   
  /** 
   * 參數(shù)帶Context 
   * @param context 
   * @param msg 
   */ 
  public static void showToast(Context context,String msg){ 
    ToastmakeText(context, msg, ToastLENGTH_SHORT)show(); 
  } 
   
  /** 
   * 調(diào)用全局的Context 
   * @param msg 
   */ 
  public static void showToast(String msg){ 
    ToastmakeText(MainApplicationgetContext(), msg, ToastLENGTH_SHORT)show(); 
  } 
} 

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

package com.tutor.application; 
 
import androidappActivity; 
import androidcontentContext; 
import androidutilAttributeSet; 
import androidviewLayoutInflater; 
import androidviewView; 
import androidwidgetButton; 
import androidwidgetFrameLayout; 
 
/** 
 * @author frankiewei 
 * 自定義的MainView 
 */ 
public class MainView extends FrameLayout implements ViewOnClickListener{ 
   
  private Context mContext; 
   
  private Activity mActivity; 
   
  /** 
   * 參數(shù)Button 
   */ 
  private Button mArgButton; 
   
  /** 
   * 全局Button 
   */ 
  private Button mGlobleButton; 
   
  /** 
   * 退出Button 
   */ 
  private Button mExitButton; 
   
  public MainView(Context context){ 
    super(context); 
    setupViews(); 
  } 
 
  public MainView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setupViews(); 
  } 
   
   
  private void setupViews(){ 
    //獲取View的上下文 
    mContext = getContext(); 
    //這里將Context轉(zhuǎn)換為Activity 
    mActivity = (Activity)mContext; 
    LayoutInflater inflater = LayoutInflaterfrom(mContext); 
    View v = inflaterinflate(Rlayoutmain, null); 
    addView(v); 
     
    mArgButton = (Button)vfindViewById(Ridarg_button); 
    mGlobleButton = (Button)vfindViewById(Ridglo_button); 
    mExitButton = (Button)vfindViewById(Ridexit_button); 
     
    mArgButtonsetOnClickListener(this); 
    mGlobleButtonsetOnClickListener(this); 
    mExitButtonsetOnClickListener(this); 
  } 
 
  public void onClick(View v) { 
    if(v == mArgButton){ 
      ToolUtilsshowToast(mContext, "我是通過傳遞Context參數(shù)顯示的!"); 
    }else if(v == mGlobleButton){ 
      ToolUtilsshowToast("我是通過全局Context顯示的!"); 
    }else{ 
      mActivityfinish(); 
    } 
  } 
 
} 

這里MainView.java使用的布局main.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemasandroidcom/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical" > 
 
  <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Welcome to frankie wei's blog"  
    /> 
   
  <Button 
    android:id="@+id/arg_button" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="傳遞Context參數(shù)" 
    /> 
   
  <Button 
    android:id="@+id/glo_button" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="全局的Context" 
    /> 
   
  <Button 
    android:id="@+id/exit_button" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="退出App" 
    /> 
 
</LinearLayout> 

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

package com.tutor.application;  
import androidappActivity; 
import androidosBundle; 
 
public class ApplicationDemoActivity extends Activity { 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    superonCreate(savedInstanceState); 
     
    MainView mMainView = new MainView(this); 
    setContentView(mMainView); 
   
  } 
   
} 

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

運行效果1                            

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android通過Path實現(xiàn)搜索按鈕和時鐘復(fù)雜效果

    Android通過Path實現(xiàn)搜索按鈕和時鐘復(fù)雜效果

    這篇文章主要為大家詳細介紹了Android通過Path實現(xiàn)搜索按鈕和時鐘復(fù)雜效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android如何實現(xiàn)時間線效果(下)

    Android如何實現(xiàn)時間線效果(下)

    上一篇文章我們講了Android如何實現(xiàn)時間線效果,今天計息上一回的文章圍繞Android實現(xiàn)時間線效果內(nèi)容展開更多,需要的朋友可以參考一下
    2021-11-11
  • Android基于高德地圖poi的仿微信獲取位置功能實例代碼

    Android基于高德地圖poi的仿微信獲取位置功能實例代碼

    這篇文章主要介紹了Android基于高德地圖poi的仿微信獲取位置功能,當(dāng)用戶打開頁面自動定位,同時搜索周邊所有poi,點擊搜索按鈕輸入關(guān)鍵字,獲取關(guān)鍵字搜索結(jié)果,本文圖文并茂給大家介紹的非常詳細,需要的朋友參考下吧
    2017-12-12
  • 詳解Android中AIDL的使用

    詳解Android中AIDL的使用

    AIDL是Android Interface definition language的縮寫,對于小白來說,AIDL的作用是讓你可以在自己的APP里綁定一個其他APP的service,這樣你的APP可以和其他APP交互,接下來通過本文給大家分享Android AIDL使用,需要的朋友參考下吧
    2021-07-07
  • Android實現(xiàn)MVVM架構(gòu)數(shù)據(jù)刷新詳解流程

    Android實現(xiàn)MVVM架構(gòu)數(shù)據(jù)刷新詳解流程

    MVVM架構(gòu)模式,即Model-View-ViewModel三個層級,MVVM模式出來的時間已經(jīng)很長了,網(wǎng)上關(guān)于MVVM模式的解析也有很多,我這里只說一下我自己的理解,基本上是和MVP模式相比較的一個差異
    2021-10-10
  • Android實現(xiàn)簡易秒表功能

    Android實現(xiàn)簡易秒表功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)簡易秒表功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Android開源組件小結(jié)

    Android開源組件小結(jié)

    Android自帶的組件比較丑陋(個人感覺),自己寫組件比較復(fù)雜,而且必須熟悉android應(yīng)用層開發(fā)的一些機制,如繪制、回調(diào),所以非迫不得已的情況下還是不要自己寫組件,因為怕考慮不周全導(dǎo)致譬如性能或異常方面的問題,你自己寫也會耗費不少時間
    2013-02-02
  • android 權(quán)限大全 分享

    android 權(quán)限大全 分享

    今天上課老師提問訪問權(quán)限,好多都沒答上來,特意收集整理了放上來
    2013-06-06
  • Android版微信跳一跳小游戲利用技術(shù)手段達到高分的操作方法

    Android版微信跳一跳小游戲利用技術(shù)手段達到高分的操作方法

    朋友圈到處都是曬微信跳一跳小游戲的,很多朋友能達到二三百分了。下面小編給大家分享Android版微信跳一跳小游戲利用技術(shù)手段達到高分的操作方法,需要的朋友一起看看吧
    2018-01-01
  • 淺談Android添加快捷方式ShortCut

    淺談Android添加快捷方式ShortCut

    這篇文章主要介紹了淺談Android添加快捷方式ShortCut,對添加快捷方式感興趣的同學(xué),可以參考下
    2021-04-04

最新評論