android获取多态方法当前活动的名称

发布于 2024-11-01 05:29:34 字数 537 浏览 0 评论 0原文

我有一个困境,基本上我有 ImageButtons,我想在所有活动上显示(已完成),并且我还希望它们以某种方式显示在所有活动上,具体取决于正在显示的活动。我希望以一种不必在每个类中单独对规范进行硬编码的方式来完成此操作。

尽管图像是在一个共享 XML 文档中定义的,但我希望共享类能够读取正在调用哪个活动,然后以某种方式格式化特定图像。这样,如果我更改功能,我只需更改一个类中的属性。

我想做某种 If 或 switch 语句来比较 if( thisActivity == MainActivity ) then,但我不知道如何检索活动的“名称”来比较它们!

编辑。我想要这样的东西:

   Context myContext = this;

    if(myContext instanceof Intent(MainMenu.class) == true)
    {

    }

我知道意图函数没有原型来检查这样的类,但我想要一些能让我知道当前类是特定类的一部分的东西,而不是其他几种情况条件语句

感谢您的任何见解!

I have a dilemma, basically I have ImageButtons that I want to be displayed on all activities (this is done) and I also want them to be displayed on all activities a certain way, depending on which activity is being displayed. I want this to be done in a way that I don't have to hardcode the specifications individually in each class.

Although the images are defined in one shared XML document, I want a shared class to be able to read which activity is being called, and then format a particular image a certain way. This is so if I change the functionality, I only have to change attributes in one class.

I want to do some sort of If or switch statement that compares if( thisActivity == MainActivity ) then, but I don't know how to retrieve the "names" of the activities to compare them!

edit. I want something like this:

   Context myContext = this;

    if(myContext instanceof Intent(MainMenu.class) == true)
    {

    }

I know the Intent function doesn't have a prototype to check a class like that, but I want something that will let me know the current class is part of a particular class, as opposed to several other cases in the conditional statement

thank you for any insight!

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

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

发布评论

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

评论(1

染年凉城似染瑾 2024-11-08 05:29:34

instanceof 运算符能满足您的需求吗?

if(this instanceof Activity1) {
//...
}
else if(this instanceof Activity2) {
//...
}

Will the instanceof operator suit your needs?

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