有人还在使用Java吗?请问我可以将这个翻译一下吗?

huangapple go评论61阅读模式
英文:

Is anyone still using Java? Can I have this translated please?

问题

Sure, here's the translation of the code you provided:

我在寻找如何检测 Android Q 设备是否启用了深色模式而我找到的唯一结果是用 Kotlin 编写的

    public boolean isDarkTheme(Activity activity) {
        return (activity.getResources().getConfiguration().uiMode &
                Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
    }

如果这是正确的那么我可以将它翻译成 Java 吗 :)
英文:

I was looking for how to detect if an Android Q device has Dark Mode enabled, and the only result I found was in Kotlin:

fun isDarkTheme(activity: Activity): Boolean {
    return activity.resources.configuration.uiMode and
            Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
}

IF it is correct, then can I please have it in Java? 有人还在使用Java吗?请问我可以将这个翻译一下吗?

答案1

得分: 2

以下是翻译好的内容:

这是Java版本

private Boolean isDarkTheme(Activity activity) {
    return (activity.getResources().getConfiguration().uiMode &
            Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}

Java getResources() -> Kotlin resources

Java getConfiguration() -> Kotlin configuration

Java & -> Kotlin and

就像你看到的一样,在Kotlin中,简单的设置器和获取器通过属性名称访问。

英文:

Here is the java Version

private Boolean isDarkTheme(Activity activity) {
    return (activity.getResources().getConfiguration().uiMode &
            Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}

Java getResources() ->Kotlin resources

Java getConfiguration() ->Kotlin configuration

Java & ->Kotlin and

as you see it's simple Setters and Getters in koltin are accessed by property name

答案2

得分: 1

我意识到对于完全不熟悉 Kotlin 的人来说,这里可能会有一些更多的内容,以防万一:

fun isDarkTheme(activity: Activity): Boolean {
    return activity.resources.configuration.uiMode and
            Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
}

所有的 get 调用在 Kotlin 中都被转换为属性语法,因此您可以像访问字段一样访问它们。其他部分只是稍微移动了一下。

英文:

I realised there might be a little more going on here to someone who's completely unfamiliar with Kotlin, so just in case:

boolean isDarkTheme(Activity activity) {
    return activity.getResources().getConfiguration().uiMode &
        Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
}

all the get calls get converted to property syntax in Kotlin, so you can just access them like fields. The rest of the stuff just moves around a bit

huangapple
  • 本文由 发表于 2020年10月18日 03:13:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64406348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定