谈谈Android里的Context的使用

上传者: carlshen | 上传时间: 2023-04-02 17:26:36 | 文件大小: 153KB | 文件类型: ZIP
大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml文件里其实是默认的 为了让大家更容易理解,写了一个简单的Demo.步骤如下: 第1步:新建一个Android工程ApplicationDemo,目录结构如下: 第2步:新建一个工具类ToolsUtil.java,代码如下 package com.tutor.application; import android.content.Context; import android.widget.Toast; /** * @author carlshen. * 应用的一些工具类. */ public class ToolUtils { /** * 参数带Context. * @param context * @param msg */ public static void showToast(Context context,String msg){ Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } /** * 调用全局的Context. * @param msg */ public static void showToast(String msg){ Toast.makeText(MainApplication.getContext(), msg, Toast.LENGTH_SHORT).show(); } } 第3步:新建一个View命名为MainView.java就是我们Activity现实的View.代码如下: package com.tutor.application; import android.app.Activity; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; /** * @author carlshen. * 自定义的MainView. */ public class MainView extends FrameLayout implements View.OnClickListener{ private Context mContext; private Activity mActivity; /** * 参数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转换为Activity. mActivity = (Activity)mContext; LayoutInflater inflater = LayoutInflater.from(mContext); View v = inflater.inflate(R.layout.main, null); addView(v); mArgButton = (Button)v.findViewById(R.id.arg_button); mGlobleButton = (Button)v.findViewById(R.id.glo_button); mExitButton = (Button)v.findViewById(R.id.exit_button); mArgButton.setOnClickListener(this); mGlobleButton.setOnClickListener(this); mExitButton.setOnClickListener(this); } public void onClick(View v) { if(v == mArgButton){ ToolUtils.showToast(mContext, "我是通过传递Context参数显示的!"); }else if(v == mGlobleButton){ ToolUtils.showToast("我是通过全局Context显示的!"); }else{ mActivity.finish(); } } } 这里MainView.java使用的布局main.xml代码如下: <?xml version="1.0" encoding="utf-8"?>

文件下载

资源详情

[{"title":"( 37 个子文件 153KB ) 谈谈Android里的Context的使用","children":[{"title":"ApplicationDemo","children":[{"title":".project <span style='color:#111;'> 851B </span>","children":null,"spread":false},{"title":"project.properties <span style='color:#111;'> 562B </span>","children":null,"spread":false},{"title":"src","children":[{"title":"com","children":[{"title":"tutor","children":[{"title":"application","children":[{"title":"MainView.java <span style='color:#111;'> 1.70KB </span>","children":null,"spread":false},{"title":"ToolUtils.java <span style='color:#111;'> 569B </span>","children":null,"spread":false},{"title":"ApplicationDemoActivity.java <span style='color:#111;'> 654B </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":"AndroidManifest.xml <span style='color:#111;'> 715B </span>","children":null,"spread":false},{"title":"res","children":[{"title":"drawable-ldpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 2.67KB </span>","children":null,"spread":false}],"spread":true},{"title":"drawable-hdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 9.18KB </span>","children":null,"spread":false}],"spread":true},{"title":"drawable-mdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 5.11KB </span>","children":null,"spread":false}],"spread":true},{"title":"layout","children":[{"title":"main.xml <span style='color:#111;'> 1020B </span>","children":null,"spread":false}],"spread":true},{"title":"drawable-xhdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 14.05KB </span>","children":null,"spread":false}],"spread":true},{"title":"values","children":[{"title":"strings.xml <span style='color:#111;'> 196B </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":".settings","children":[{"title":"org.eclipse.jdt.core.prefs <span style='color:#111;'> 177B </span>","children":null,"spread":false},{"title":"org.eclipse.core.resources.prefs <span style='color:#111;'> 86B </span>","children":null,"spread":false}],"spread":true},{"title":"assets","children":null,"spread":false},{"title":"gen","children":[{"title":"com","children":[{"title":"tutor","children":[{"title":"application","children":[{"title":"BuildConfig.java <span style='color:#111;'> 163B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 862B </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":".classpath <span style='color:#111;'> 364B </span>","children":null,"spread":false},{"title":"proguard-project.txt <span style='color:#111;'> 781B </span>","children":null,"spread":false},{"title":"bin","children":[{"title":"resources.ap_ <span style='color:#111;'> 33.87KB </span>","children":null,"spread":false},{"title":"classes","children":[{"title":"com","children":[{"title":"tutor","children":[{"title":"application","children":[{"title":"R$layout.class <span style='color:#111;'> 403B </span>","children":null,"spread":false},{"title":"MainView.class <span style='color:#111;'> 2.29KB </span>","children":null,"spread":false},{"title":"BuildConfig.class <span style='color:#111;'> 351B </span>","children":null,"spread":false},{"title":"R$drawable.class <span style='color:#111;'> 416B </span>","children":null,"spread":false},{"title":"R$attr.class <span style='color:#111;'> 349B </span>","children":null,"spread":false},{"title":"ApplicationDemoActivity.class <span style='color:#111;'> 910B </span>","children":null,"spread":false},{"title":"R$id.class <span style='color:#111;'> 466B </span>","children":null,"spread":false},{"title":"ToolUtils.class <span style='color:#111;'> 843B </span>","children":null,"spread":false},{"title":"R.class <span style='color:#111;'> 552B </span>","children":null,"spread":false},{"title":"R$string.class <span style='color:#111;'> 436B </span>","children":null,"spread":false}],"spread":false}],"spread":false}],"spread":false}],"spread":true},{"title":"AndroidManifest.xml <span style='color:#111;'> 715B </span>","children":null,"spread":false},{"title":"jarlist.cache <span style='color:#111;'> 119B </span>","children":null,"spread":false},{"title":"res","children":[{"title":"drawable-ldpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 2.60KB </span>","children":null,"spread":false}],"spread":false},{"title":"drawable-hdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 8.98KB </span>","children":null,"spread":false}],"spread":false},{"title":"drawable-mdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 4.94KB </span>","children":null,"spread":false}],"spread":false},{"title":"drawable-xhdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 13.74KB </span>","children":null,"spread":false}],"spread":false}],"spread":false},{"title":"classes.dex <span style='color:#111;'> 5.30KB </span>","children":null,"spread":false},{"title":"ApplicationDemo.apk <span style='color:#111;'> 38.27KB </span>","children":null,"spread":false}],"spread":true}],"spread":false}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明