本文介绍了怎样的android创建自己的活动,并延长了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从它创建一个继承基类活动这确实在我的应用程序的一些常见任务并致以活动,在格式如下:

I need to create a base class that extends Activity which does some common tasks in my application and extend my activities from it,in the following form:

公共BaseActivity延伸活动{...}

public BaseActivity extends Activity{....}

公共子活动延伸BaseActivity {...}

public SubActivity extends BaseActivity{...}

子活动我需要给在 BaseActivity ,我可能需要定义不同的布局对于子活动根据一些标志值,也(在子活动)我想执行一个在定义的AsyncTask BaseActivity

in SubActivity I need to give values to some variables and UI components defined in BaseActivity, I may need to define a different layout for SubActivity according to some flag value, also(in SubActivity ) I want to execute asyncTask that is defined in BaseActivity.

这可能吗?如果是,是否有任何教程,可以帮助?谢谢你在前进

is this possible? if yes, is there any tutorial that may help?thank you in advance

推荐答案

究竟是你想达到什么目的?具有两个不同的活动具有共同的ui,除了一些变量的布局或部分

What exactly are you trying to achieve? Having two different activities with a common ui, except for some variables or parts of the layout?

在这种情况下,我建议有一个抽象基活动,和两个具体子类继承。您可以定义所有的共同行为的基本活动,并有抽象方法的差异,然后您再覆盖在你的实际实现。

In this case, I suggest having a base abstract activity, and two concrete inherited subclasses. You define all the common behaviour in the base activity, and have abstract methods for the differences, which you then override in your actual implementations.

例如,两个活动不同的布局资源:

For example, for two activities with different layout resources:

public abstract class BaseActivity extends Activity {
    @Override
    public void onCreate(bundle) {
        super.onCreate(bundle);
        setContentView(getLayoutResourceId());
    }

    protected abstract int getLayoutResourceId();
}

public class Activity1 extends BaseActivity {
    @Override
    public void onCreate(bundle) {
        super.onCreate(bundle);
        // do extra stuff on your resources, using findViewById on your layout_for_activity1
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.layout_for_activity1;
    }
}

您可以有很多更抽象的方法,为每一位想要具体到你的子类。

You can have a lot more abstract methods, for every bit you want specific to your subclasses.

做的是,在我看来,很多比有一个具体的子类,以一个具体的超好:可导致很多问题,通常是难以调试

Doing that is, in my opinion, a lot better than having a concrete subclass to a concrete superclass: that can lead to many problems and is usually difficult to debug.

这篇关于怎样的android创建自己的活动,并延长了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:24