如何创建共同code为Android的活动部件? [英] How do I create common code for parts of Android activities?

查看:145
本文介绍了如何创建共同code为Android的活动部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序有14个活动。指出,9活性包含自定义标题栏和标签窗格。所以在这里我需要在一个地方,而不是冗余code在每个包含自定义标题栏和标签面板code活性写这个共同code(即布局和它的活动的具体code)

In my application there are 14 activities. Out of that 9 activity contains custom title bar and tab pane. so here I need to write this common code at one place instead of redundant code in each activity that contain custom title bar and tab pane code (i.e layout and it's activity specific code)

什么是可能的方式来做到这一点?

What are the possible ways to do this?

推荐答案

的常用方法是:

  • 创建一个超类调用,例如, CommonActivity 延伸活动
  • 把样板code这个类里面
  • 然后,让你的活动延长 CommonActivity 而不是活动
  • Create a super class called, for instance, CommonActivity which extends Activity
  • Put the boilerplate code inside that class
  • Then make your activities extend CommonActivity instead of Activity:

下面一个简单的例子:

public class CommonActivity extends Activity{
    public void onCreate(Bundle b){
        super.onCreate(b);
        // code that is repeated
    }

    protected void moreRepeatitiveCode(){
    }
}

和你当前活动:

public class AnActivity extends CommonActivity{
    public void onCreate(Bundle b){
        super.onCreate(b);
        // specific code
    }
}

这篇关于如何创建共同code为Android的活动部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆