为你的Android应用定制属于你的BaseActivity http://blog.youkuaiyun.com/jiahui524

自定义Android应用BaseActivity
本文分享了如何为Android应用创建一个自定义的BaseActivity,该BaseActivity封装了通用代码,简化了应用开发过程,提高了代码复用性和维护性。

为你的Android应用定制属于你的BaseActivity

2013年08月28日  ⁄ 综合 ⁄ 共 3297字 ⁄ 字号  小 中 大  ⁄ 评论关闭
id="iframeu1788635_0" src="http://pos.baidu.com/acom?rdid=1788635&dc=2&di=u1788635&dri=0&dis=0&dai=2&ps=236x909&dcb=BAIDU_UNION_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1456420718524&ti=%E4%B8%BA%E4%BD%A0%E7%9A%84Android%E5%BA%94%E7%94%A8%E5%AE%9A%E5%88%B6%E5%B1%9E%E4%BA%8E%E4%BD%A0%E7%9A%84BaseActivity%20%7C%20%E5%AD%A6%E6%AD%A5%E5%9B%AD&ari=1&dbv=2&drs=1&pcs=1366x607&pss=1366x256&cfv=0&cpl=26&chi=1&cce=true&cec=UTF-8&tlm=1456420718&ltu=http%3A%2F%2Fwww.xuebuyuan.com%2F651917.html&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3D8L320J_B5oxY3CamGKZqEefk-zP9VxqWehutSSHfgxFZ5Oxwt3oKALNiQQLRrjNg%26wd%3D%26eqid%3Dd4ab453f000147850000000256cf35b6&ecd=1&psr=1366x768&par=1366x724&pis=-1x-1&ccd=24&cja=true&cmi=49&col=zh-CN&cdo=-1&tcn=1456420719&qn=df5ca33385564dc0&tt=1456420718497.67.230.231" width="336" height="280" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial; vertical-align: bottom; background: transparent;">

 相信大家在开发Android应用的过程中肯定碰到过很多重复的工作,写着重复的代码,有时候连布局文件也是一样,需要重复的劳动,那么这样对于我们程序来讲肯定是很累很繁琐的一件事,所以我们在写代码的时候是否需要去考虑让我们写更少的代码,程序员要学会偷懒,否则……..

在开发应用程序的时候我们的设计其实整体的样式是统一,那么我们就可以写一些公用的代码,这样对程序来讲也便于后面的维护,废话也不多说了,相信大家肯定也懂的,今天我分享给大家的就是定制一个属于自己的BaseActivity,这个BaseActivity主要封装了一些公用的代码,例如我们在开发过程中上面的那些标题和按钮肯定都要有的,所以我们可以把这些公用的都写在这个BaseActivity里,其他功能的Activity以后都继承这个BaseActivity.

 

先上效果图

                

 

 

 

效果图看了,大家是否有所启发或是有所了解呢?那么接下来就放BaseActivity里的核心代码咯:

/**
 * 继承于Activity用于以后方便管理
 * 
 * @author coder
 * 
 */
public class BaseActivity extends Activity {

	private View titleView;
	private TextView tv_title;
	private Button btn_left, btn_right;

	private LinearLayout ly_content;
	// 内容区域的布局
	private View contentView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.common_title);
		titleView = findViewById(R.id.titleView);
		tv_title = (TextView) titleView.findViewById(R.id.tv_title);
		btn_left = (Button) titleView.findViewById(R.id.btn_left);
		btn_right = (Button) titleView.findViewById(R.id.btn_right);

		ly_content = (LinearLayout) findViewById(R.id.ly_content);
	}

	/***
	 * 设置内容区域
	 * 
	 * @param resId
	 *            资源文件ID
	 */
	public void setContentLayout(int resId) {

		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		contentView = inflater.inflate(resId, null);
		LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		contentView.setLayoutParams(layoutParams);
		contentView.setBackgroundDrawable(null);
		if (null != ly_content) {
			ly_content.addView(contentView);
		}

	}

	/***
	 * 设置内容区域
	 * 
	 * @param view
	 *            View对象
	 */
	public void setContentLayout(View view) {
		if (null != ly_content) {
			ly_content.addView(view);
		}
	}

	/**
	 * 得到内容的View
	 * 
	 * @return
	 */
	public View getLyContentView() {

		return contentView;
	}

	/**
	 * 得到左边的按钮
	 * 
	 * @return
	 */
	public Button getbtn_left() {
		return btn_left;
	}

	/**
	 * 得到右边的按钮
	 * 
	 * @return
	 */
	public Button getbtn_right() {
		return btn_right;
	}

	/**
	 * 设置标题
	 * 
	 * @param title
	 */
	public void setTitle(String title) {

		if (null != tv_title) {
			tv_title.setText(title);
		}

	}

	/**
	 * 设置标题
	 * 
	 * @param resId
	 */
	public void setTitle(int resId) {
		tv_title.setText(getString(resId));
	}

	/**
	 * 设置左边按钮的图片资源
	 * 
	 * @param resId
	 */
	public void setbtn_leftRes(int resId) {
		if (null != btn_left) {
			btn_left.setBackgroundResource(resId);
		}

	}

	/**
	 * 设置左边按钮的图片资源
	 * 
	 * @param bm
	 */
	public void setbtn_leftRes(Drawable drawable) {
		if (null != btn_left) {
			btn_left.setBackgroundDrawable(drawable);
		}

	}

	/**
	 * 设置右边按钮的图片资源
	 * 
	 * @param resId
	 */
	public void setbtn_rightRes(int resId) {
		if (null != btn_right) {
			btn_right.setBackgroundResource(resId);
		}
	}

	/**
	 * 设置右边按钮的图片资源
	 * 
	 * @param drawable
	 */
	public void setbtn_rightRes(Drawable drawable) {
		if (null != btn_right) {
			btn_right.setBackgroundDrawable(drawable);
		}
	}

	/**
	 * 隐藏上方的标题栏
	 */
	public void hideTitleView() {

		if (null != titleView) {
			titleView.setVisibility(View.GONE);
		}
	}

	/**
	 * 隐藏左边的按钮
	 */
	public void hidebtn_left() {

		if (null != btn_left) {
			btn_left.setVisibility(View.GONE);
		}

	}

	/***
	 * 隐藏右边的按钮
	 */
	public void hidebtn_right() {
		if (null != btn_right) {
			btn_right.setVisibility(View.GONE);
		}

	}

	public BaseActivity() {

	}

}

接下来再给出其中的一个用法就可以了:

public class TwoBtnActivity extends BaseActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentLayout(R.layout.two);

		//设置标题
		setTitle("两个按钮");



		// 为左边的按钮增加监听事件
		getbtn_left().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();

			}
		});

	}

}


好了大功告成了,这个万能的BaseActivity是不是很好用呀,希望这样的一个小小的分享能对大家有所帮助咯

如需转载引用请注明出处:http://blog.youkuaiyun.com/jiahui524

compiler message file broken: key=compiler.misc.msg.bug arguments=11.0.27, {1}, {2}, {3}, {4}, {5}, {6}, {7} java.lang.NullPointerException at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitApply(Flow.java:1235) at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1650) at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49) at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:398) at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.visitExec(TreeScanner.java:213) at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCExpressionStatement.accept(JCTree.java:1468) at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49) at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:398) at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:57) at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitBlock(Flow.java:997) at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1036) at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49) at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:398) at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitMethodDef(Flow.java:964) at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:866) at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49) at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:398) at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitClassDef(Flow.java:927) at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:774) at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49) at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:398) at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.analyzeTree(Flow.java:1327) at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.analyzeTree(Flow.java:1317) at jdk.compiler/com.sun.tools.javac.comp.Flow.analyzeTree(Flow.java:218) at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1400) at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1374) at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:973) at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:104) at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.handleExceptions(JavacTaskImpl.java:147) at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:100) at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:94) @Before public void setUp() { ActivityController<TestBaseActivity> controller = Robolectric.buildActivity(TestBaseActivity.class); tbaseActivity = controller.get(); controller.create().start(); } 这个代码报错
07-30
07-21 20:07:15.157 5376 5376 D SourceMng_Manager_20200401: SrcMngAudioSwitchManager abandonAdayoAudioFocus() sourceType = ADAYO_SOURCE_MEDIA id = com.adayo.proxy.infrastructure.sourcemng.Control.SrcMngAudioSwitchManager@f592723com.adayo.medialibrary.util.DeviceServiceProxy$2@af48420 行 339179: 07-21 20:07:15.158 5376 5376 D SourceMng_Manager_20200401: SrcMngAudioSwitchManager abandonAdayoAudioFocus() end abandonRlt = 1 行 339181: 07-21 20:07:15.159 5376 5376 D MediaLibrary<2507201054> VideoPlaybackService: (VideoPlaybackService.java:35)#SetVideoPlaying pre-> [VideoPreviewActivity.java:406#onPause] info->: setVideoPlaying isPlaying false 行 339183: 07-21 20:07:15.159 5376 5376 I am_on_paused_called: [0,com.adayo.medialibrary.ui.VideoPreviewActivity,performPause] 行 339301: 07-21 20:07:15.187 5376 5376 I Adayo-Mvvm-BaseActivity: com.haibing.mvvm.bases.ui.BaseActivity==>onWindowFocusChanged(BaseActivity.java:125): class com.adayo.medialibrary.ui.VideoPreviewActivity==>onWindowFocusChanged hasFocus = false 行 339302: 07-21 20:07:15.188 5376 5376 D MediaLibrary<2507201054> VideoPreviewActivity: (VideoPreviewActivity.java:1580)#OnWindowFocusChanged pre-> [WindowCallbackWrapper.java:125#onWindowFocusChanged] info->: onWindowFocusChanged: hasFocus false 行 342275: 07-21 20:07:17.379 5376 5376 D IJKMEDIA: IjkMediaPlayer_setVideoSurface 行 342276: 07-21 20:07:17.379 5376 5376 D IJKMEDIA: ijkmp_set_android_surface(surface=0x0) 行 342277: 07-21 20:07:17.379 5376 5376 D IJKMEDIA: ffpipeline_set_surface() 行 342278: 07-21 20:07:17.379 5376 5376 D IJKMEDIA: ijkmp_set_android_surface(surface=0x0)=void 行 342311: 07-21 20:07:17.421 5376 5376 D ConnectivityManager: StackLog: [android.net.ConnectivityManager.unregisterNetworkCallback(ConnectivityManager.java:4029)] [com.bumptech.glide.manager.SingletonConnectivityReceiver$FrameworkConnectivityMonitorPostApi24.unregister(SingletonConnectivityReceiver.java:205)] [com.bumptech.glide.manager.SingletonConnectivityReceiver.maybeUnregisterReceiver(SingletonConnectivityReceiver.java:122)] [com.bumptech.glide.manager.SingletonConnectivityReceiver.unregister(SingletonConnectivityReceiver.java:105)] [com.bumptech.glide.manager.DefaultConnectivityMonitor.unregister(DefaultConnectivityMonitor.java:29)] [com.bumptech.glide.manager.DefaultConnectivityMonitor.onStop(DefaultConnectivityMonitor.java:39)] [com.bumptech.glide.manager.LifecycleLifecycle.onStop(LifecycleLifecycle.java:35)] [java.lang.reflect.Method.invoke(Native Method)] [androidx.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:225)] [androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(ClassesInfoCache.java:199)] [androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(ClassesInfoCache.java:190)] [androidx.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:40)] [androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:360)] [androidx.lifecycle.LifecycleRegistry.backwardPass(LifecycleRegistry.java:290)] [androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:308)] [androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:151)] [androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)] [androidx.lifecycle.ReportFragment.dispatch(ReportFragment.java:68)] [androidx.lifecycle.ReportFragment$LifecycleCallbacks.onActivityPreStopped(ReportFragment.java:210)] [android.app.Activity.dispatchActivityPreStopped(Activity.java:1328)] [android.app.Activity.performStop(Activity.java:8033)] [android.app.ActivityThread.callActivity ... 行 342322: 07-21 20:07:17.424 5376 5376 I Adayo-Mvvm-BaseActivity: com.haibing.mvvm.bases.ui.BaseActivity==>onStop(BaseActivity.java:98): class com.adayo.medialibrary.ui.VideoPreviewActivity==>onStop 行 342323: 07-21 20:07:17.424 5376 5376 I am_on_stop_called: [0,com.adayo.medialibrary.ui.VideoPreviewActivity,STOP_ACTIVITY_ITEM] 行 342324: 07-21 20:07:17.424 5376 5376 D MediaLibrary<2507201054> VideoPreviewActivity: (VideoPreviewActivity.java:746)#OnSaveInstanceState pre-> [Activity.java:2048#performSaveInstanceState] info->: onSaveInstanceState() 行 342325: 07-21 20:07:17.424 5376 5376 D MediaLibrary<2507201054> VideoPreviewActivity.java: (VideoPreviewActivity.java:1548)#SaveLastPlayPosition pre-> [VideoPreviewActivity.java:753#onSaveInstanceState] info->: saveLastPlayPosition sCurrentVideoPath /storage/8C3C-7AC3/视频符号名称/MP4/那些你很冒险的梦-art--林俊杰--art-4ddd1cebd2065ed97b08c8bb84036768.mp4, sCurrentSeekPosition 67303 行 342326: 07-21 20:07:17.425 5376 5376 I MediaLibrary<2507201054> VideoPreviewActivity: (VideoPreviewActivity.java:755)#OnSaveInstanceState pre-> [Activity.java:2048#performSaveInstanceState] info->: onSaveInstanceState mCurrentPosition:9 sCurrentSeekPosition:67303 行 342329: 07-21 20:07:17.426 5376 5376 I MediaLibrary<2507201054> VideoPreviewActivity: (VideoPreviewActivity.java:756)#OnSaveInstanceState pre-> [Activity.java:2048#performSaveInstanceState] info->: onSaveInstanceState: playState = 5 行 344098: 07-21 20:07:18.971 5376 5376 I Adayo-Mvvm-BaseActivity: com.haibing.mvvm.bases.ui.BaseActivity==>onRestart(BaseActivity.java:79): class com.adayo.medialibrary.ui.VideoPreviewActivity==>onRestart 行 344099: 07-21 20:07:18.971 5376 5376 I am_on_restart_called: [0,com.adayo.medialibrary.ui.VideoPreviewActivity,performRestartActivity] 行 344107: 07-21 20:07:18.992 5376 5376 I Adayo-Mvvm-BaseActivity: com.haibing.mvvm.bases.ui.BaseActivity==>onStart(BaseActivity.java:73): class com.adayo.medialibrary.ui.VideoPreviewActivity==>onStart 行 344108: 07-21 20:07:18.992 5376 5376 I am_on_start_called: [0,com.adayo.medialibrary.ui.VideoPreviewActivity,handleStartActivity]
最新发布
08-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值