gpt4 book ai didi

android - 切换案例中的 Activity 上下文

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:20 25 4
gpt4 key购买 nike

我想使用在所有 Activity 中使用的通用进度条。这可以通过检查 if else 语句来完成,例如

if(mContext instanceOf ActivityA)
{
//Do Something
}else if(mContext instanceOf ActivityB)
{
//Do Something
}

但我想做类似的事情:

switch(mContext){
case mContext instaceOf ActivityA:
//Do Something
case mContext instanceOf ActivityB:
//DoSomething
}

如何通过检查 switch 中的上下文来实现

最佳答案

你可以这样做:

String className = mContext.getClass().getSimpleName();

switch (className) {
case "ActivityA":
//Do Something
case "ActivityB":
//DoSomething
}

注意:仅在 JDK 7 之后才添加了对 switch 的字符串支持。如果您使用的是早期版本的 Java,则可以在上面使用一些技巧来使 switch 正常工作。 this question 的答案中的一些示例.

关于android - 切换案例中的 Activity 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34565026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com