最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

其實(shí)我們應(yīng)用啟動(dòng)的時(shí)候會(huì)啟動(dòng)Application這個(gè)類,這個(gè)類是在AndroidManifest.xml文件里其實(shí)是默認(rèn)的

<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> 

這個(gè)Application類是單例的,也就是說我們可以自己寫個(gè)Application(比如名為:MainApplication)類,來代替默認(rèn)的Applicaiton,這個(gè)類可以保存應(yīng)用的全局變量,我們可以定義一個(gè)全局的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注冊(cè)進(jìn)去(第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> 

為了讓大家更容易理解,寫了一個(gè)簡(jiǎn)單的Demo.步驟如下:

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

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

第三步:新建一個(gè)工具類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(); 
  } 
} 

第四步:新建一個(gè)View命名為MainView.java就是我們Activity現(xiàn)實(shí)的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); 
   
  } 
   
} 

第六步:運(yùn)行上述工程效果如下:

運(yùn)行效果1                            

運(yùn)行效果2---- 點(diǎn)擊第一個(gè)按鈕 

運(yùn)行效果3---- 點(diǎn)擊第二個(gè)按鈕

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

相關(guān)文章

  • Android自定義View實(shí)現(xiàn)縱向跑馬燈效果詳解

    Android自定義View實(shí)現(xiàn)縱向跑馬燈效果詳解

    對(duì)于跑馬燈效果在我們?nèi)粘J褂玫腶pp中還是很常見的,比如外賣app的商家公告就使用了此效果,但是它是橫向滾動(dòng)的,橫向滾動(dòng)多適用于單條信息;但凡涉及到多條信息的滾動(dòng)展示,用縱向滾動(dòng)效果會(huì)有更好的用戶體驗(yàn),今天我們通過自定義View來看看如何實(shí)現(xiàn)縱向跑馬燈效果。
    2016-11-11
  • Android設(shè)備藍(lán)牙連接掃描槍獲取掃描內(nèi)容

    Android設(shè)備藍(lán)牙連接掃描槍獲取掃描內(nèi)容

    這篇文章主要為大家詳細(xì)介紹了Android設(shè)備藍(lán)牙連接掃描槍獲取掃描內(nèi)容,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android中監(jiān)聽系統(tǒng)網(wǎng)絡(luò)連接打開或者關(guān)閉的實(shí)現(xiàn)代碼

    Android中監(jiān)聽系統(tǒng)網(wǎng)絡(luò)連接打開或者關(guān)閉的實(shí)現(xiàn)代碼

    本篇文章對(duì)Android中監(jiān)聽系統(tǒng)網(wǎng)絡(luò)連接打開或者關(guān)閉的實(shí)現(xiàn)用實(shí)例進(jìn)行了介紹。需要的朋友參考下
    2013-05-05
  • 最新評(píng)論

    调兵山市| 文成县| 虞城县| 余姚市| 辛集市| 玉屏| 龙州县| 宜丰县| 辽阳县| 平遥县| 佛坪县| 抚松县| 砚山县| 巴彦县| 邯郸市| 商丘市| 大英县| 留坝县| 昌平区| 荆州市| 潮州市| 内丘县| 安多县| 民丰县| 三原县| 株洲市| 宜兴市| 阿鲁科尔沁旗| 驻马店市| 谢通门县| 舟曲县| 宕昌县| 合作市| 麻城市| 叶城县| 萨嘎县| 祁东县| 板桥市| 白水县| 同江市| 隆德县|