扩展类活动 [英] Extending class for activity

查看:128
本文介绍了扩展类活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是全新到Android(Java)的发展,我很兴奋!谷歌的开发者指南是太棒了,我在小的时候学到了很多东西。它甚至在夜间保持我清醒;)

I'm totally new to Android (Java) Development and I'm so excited about it! The developers guide of Google is fantastic and I learned a lot in a small time. It even keeps me awake during the night ;)

今天,我经历了制作菜单的有件事情,我无法理解。这是关于扩展类。导游说:

Today I went through making menu's and there's something that I can't understand. It's about extending classes. The guide says:

提示:如果您的应用程序包含多种活动,其中一些   提供相同的选项菜单,请考虑创建一个活动,   实现只按onCreateOptionsMenu()和   onOptionsItemSelected()方法。然后扩展此类每个   活动应该共享相同的选项菜单。这样一来,你有   为处理菜单操作,每个只管理一组code   子类继承了菜单的行为。

Tip: If your application contains multiple activities and some of them provide the same Options Menu, consider creating an activity that implements nothing except the onCreateOptionsMenu() and onOptionsItemSelected() methods. Then extend this class for each activity that should share the same Options Menu. This way, you have to manage only one set of code for handling menu actions and each descendant class inherits the menu behaviors.

的时候,我不明白的是如何扩展一个类......让说我有一个 MainActivity 子活动。我想有两个业务相同的菜单,以便我做出的 MainMenuActivity 。如何扩展这个类为活动的?

The point I don't get is how to extend a class... Let say I have a MainActivity and a SubActivity. I want to have the same menu in both activities so I make a MainMenuActivity. How do I extend this class for both activity's?

是我在网上搜索,但没有找到任何有用的。我真的想了解它,所以我希望有人能帮助我与一些示例code +的解释。预先感谢您!!

Yes I do searched on the net but couldn't find any usable. I really want to understand it so I hope anyone can help me out with some samplecode + explanation. Thank you in advance!!

推荐答案

他们的意思是这样的:

通常情况下,你将有:

public class MyActivity extends Activity{...}

如果您有4-5-6的活动......,他们每个人都使用相同的菜单code,你可以只复制并粘贴code 4-5-6 ..倍。或者,你可以这样做:

If you have 4-5-6... of those activities, and each of them uses the same menu code, you could just copy and paste the code 4-5-6.. times. Or you could do this:

public class BaseActivity extends Activity{

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //My menu code  
    }
}

和使用这个类为您的4-5-6 ...活动:

And use this class for your 4-5-6... Activities:

public class MyActivity extends BaseActivity{...}

这样,您就不需要复制你的菜单创造code到所有的活动,而且,你不必编辑4-5-6 ...类编辑创造的一个小一点菜单。菜单code是现在也在 MyActivity

您也可以在这里看看,它解释了什么是扩展表示。

这篇关于扩展类活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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