使用Kotlin组合整数标志的最佳方法? [英] Best way to combine integer flags using Kotlin?

查看:44
本文介绍了使用Kotlin组合整数标志的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我们经常通过|组合标志.运算符.

In java we regularly combine flags via the | operator.

例如

getWindow().getDecorView().setSystemUiVisibility(
  View.SYSTEM_UI_FLAG_LAYOUT_STABLE | 
  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | 
  View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
);

我似乎在Kotlin中找不到等效的运算符.有谁知道在Kotlin中组合整数标志的便捷方法吗?

I can't seem to find the equivalent operator in Kotlin. Anyone know a convenient way to combine integer flags in Kotlin?

推荐答案

只需使用 or :

getWindow().getDecorView().setSystemUiVisibility(
  View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
  View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
);

这可能有点令人困惑.您可以创建一个辅助扩展功能with(或其他任何功能)以使其更具可读性:

This may be a little confusing. You can create a little helper extension function with (or whatever) to make it more readable:

infix fun Int.with(x: Int) = this.or(x)

getWindow().getDecorView().setSystemUiVisibility(
  View.SYSTEM_UI_FLAG_LAYOUT_STABLE with
  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION with
  View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
);

这篇关于使用Kotlin组合整数标志的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆