仅供参考,这是我在StackOverflow上的第一个问题,我还是个新手.

在处理一个完全是Kotlin(版本1.1.3-2)的项目时,我在以下代码中看到一条警告(带给好奇的小伙子们的注释):

    // Code below is to handle presses of Volume up or Volume down.
    // Without this, after pressing volume buttons, the navigation bar will
    // show up and won't hide
    val decorView = window.decorView
    decorView
        .setOnSystemUiVisibilityChangeListener { visibility ->
            if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN === 0) {
                 decorView.systemUiVisibility = flags
            }
        }

The warning is for visibility and View.SYSTEM_UI_FLAG_FULLSCREEN === 0, and it says Identity equality for arguments of types Int and Int is deprecated.

我应该如何更改代码,为什么它一开始就被弃用(出于了解的原因)?

推荐答案

You can change the code by using structual equality instead as below:

//              use structual equality instead ---v
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
    decorView.systemUiVisibility = flags
}

Why don't suggest to use referential equality? you can see my answer here.

On the other hand, when you use referential/identity equality maybe return false, for example:

val ranged = arrayListOf(127, 127)

println(ranged[0] === ranged[1]) // true
println(ranged[0] ==  ranged[1]) // true

val exclusive = arrayListOf(128, 128)

//                                        v--- print `false` here
println(exclusive[0] === exclusive[1]) // false
println(exclusive[0] ==  exclusive[1]) // true

Kotlin相关问答推荐

如何将时间值格式化为00:00和00:00:00 Kotlin?""""

如何在Kotlin中为接受varargs的函数添加带有默认值的新参数?

编译后的JavaFX应用程序立即以静默方式崩溃

用于将 0.5 变为 0 的 round() 函数的模拟

如何在 Android Jetpack Compose 中的画布上绘制一侧加厚的描边?

从 Kotlin 调用 Java 时可以为空的规则是什么

如何连接两个 kotlin 流?

在 Kotlin 中,::class.simpleName是做什么的?

Kotlin 从其他类调用成员扩展函数

如何从kotlin中的ArrayList中删除所有元素

调用单元测试验证伴随对象方法

Kotlin 具体化的泛型不会按计划保持类型

使用 Paging 3 时保存并保留 LazyColumn 滚动位置

Android:在 DAO 中使用 Room 数据库和 LiveData 架构

Kotlin:什么是 kotlin.String!类型

Kotlin使用运行时断言进行空判断?

在 Kotlin 中创建 Spinner 时,如何在 Fragment 中的旋转屏幕上修复指定为非空的参数为空?

重复构建失败To use Coroutine features, you must add `ktx`......

无法解决:androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1

内联 Kotlin 方法没有覆盖报告