<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>
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();
}
}
<?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>
package com.tutor.application;
import androidcontentContext;
import androidwidgetToast;
/**
* @author frankiewei
* 应用的一些工具类
*/
public class ToolUtils {
/**
* 参数带Context
* @param context
* @param msg
*/
public static void showToast(Context context,String msg){
ToastmakeText(context, msg, ToastLENGTH_SHORT)show();
}
/**
* 调用全局的Context
* @param msg
*/
public static void showToast(String msg){
ToastmakeText(MainApplicationgetContext(), msg, ToastLENGTH_SHORT)show();
}
}
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;
/**
* 参数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 = 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参数显示的!");
}else if(v == mGlobleButton){
ToolUtilsshowToast("我是通过全局Context显示的!");
}else{
mActivityfinish();
}
}
}
<?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参数"
/>
<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>
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);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有