Android 中的嵌套内部 Activity 类

发布于 2024-09-30 07:56:45 字数 596 浏览 3 评论 0原文

是否可以在另一个 Activity 类中声明一个扩展 Activity 的类?如果是,我将如何在清单中注册该类?另外,这是可以合理完成的事情还是一个坏主意?

我在想类似的东西,

class ListClass extends ListActivity{

    ...
    ArrayList items;

    class ItemClass extends Activity{

        ...
        Item item;

        @Override
        onCreate(){
            Integer pos = getIntent().getExtras().getInt("pos");
            item = items.get(pos);
        }
    }

    @Override
    onItemClick(int position){

        startActivity(new Intent(this, ItemClass.class).putExtra("pos", position));

    }
}

注意语法显然不是 100% 正确,主要是伪代码。

Is declaring a class that extends Activity inside another Activity class possible? If it is, how would I register that class in the manifest? Also, is that something that can be reasonably done or is it a bad idea?

I was thinking of something like

class ListClass extends ListActivity{

    ...
    ArrayList items;

    class ItemClass extends Activity{

        ...
        Item item;

        @Override
        onCreate(){
            Integer pos = getIntent().getExtras().getInt("pos");
            item = items.get(pos);
        }
    }

    @Override
    onItemClick(int position){

        startActivity(new Intent(this, ItemClass.class).putExtra("pos", position));

    }
}

Note the syntax isn't 100% correct obviously, mostly pseudocode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时模样 2024-10-07 07:56:45

是的,它确实有效——它只是另一个类——你只需要在 AndroidManifest.xml 中使用内部类表示法来声明你的活动:

似乎对我来说工作得很好,但也许当这个问题被问到时,旧版本的 Android 不支持它?

不确定你为什么要这样做,但你可以。

Yes, it does work -- it is just another class -- you just need to declare your activity using inner class notation in AndroidManifest.xml:

<activity android:name=".ListClass$ItemClass"/>

Seems to work fine for me, but perhaps when this question was asked, it wasn't supported in older versions of Android?

Not sure WHY you'd want to do this, but you can.

手心的温暖 2024-10-07 07:56:45

我也很好奇你为什么要这样做。

但是,我看不出有任何理由它不起作用。只要两个类都是公共的,您就不能像平常一样在 AndroidManifest 中引用它吗?即 com.falmarri.ListClass.ItemClass?

编辑:没关系,这并不像 EboMike 指出的那样起作用。

I'd also be curious why you'd want to do this.

However, I don't see any reason why it wouldn't work. Couldn't you reference it in the AndroidManifest as you normally would as long as both classes are public? i.e. com.falmarri.ListClass.ItemClass?

Edit: Nevermind, this doesn't work as EboMike pointed out.

月亮是我掰弯的 2024-10-07 07:56:45

不,那不可能。毕竟,如果 Activity 在任何时候启动(例如,如果您通过 Intent 启动它),Android 操作系统都需要实例化 Activity,并且在没有父项 ItemClass 的情况下不可能实例化 ItemClass代码>ListClass。

请记住,每个活动都是完全独立的,可以通过意图在任何时候启动。

No, that's not possible. After all, the Android operating system will need to instantiate the Activity if it is started at any point (say, if you start it through an intent), and it's impossible to instantiate an ItemClass without a parent ListClass.

Remember that each Activity is completely independent and can be started at any point through an intent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文