英文:
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?
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论