相信大家在开发Android
应用的过程中肯定碰到过很多重复的工作,
写着重复的代码,
有时候连布局文件也是一样,
需要重复的劳动,
那么这样对于我们程序来讲肯定是很累很繁琐的一件事,
所以我们在写代码的时候是否需要去考虑让我们写更少的代码,
程序员要学会偷懒,
否则……..
在开发应用程序的时候我们的设计其实整体的样式是统一,
那么我们就可以写一些公用的代码,
这样对程序来讲也便于后面的维护,
废话也不多说了,
相信大家肯定也懂的,
今天我分享给大家的就是定制一个属于自己的BaseActivity,
这个BaseActivity
主要封装了一些公用的代码,
例如我们在开发过程中上面的那些标题和按钮肯定都要有的,
所以我们可以把这些公用的都写在这个BaseActivity
里,
其他功能的Activity
以后都继承这个BaseActivity.
先上效果图
效果图看了,
大家是否有所启发或是有所了解呢?
那么接下来就放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
是不是很好用呀,
希望这样的一个小小的分享能对大家有所帮助咯
|