android - 用 '&' 切换两个值

标签 android switch-statement

   switch (attribute.inputType & EditorInfo.TYPE_MASK_CLASS) {
            case EditorInfo.TYPE_CLASS_NUMBER:
            case EditorInfo.TYPE_CLASS_DATETIME:
            case EditorInfo.TYPE_CLASS_PHONE:
                mCurKeyboard = mSymbolsKeyboard;
                break;

            case EditorInfo.TYPE_CLASS_TEXT: 
                mCurKeyboard = mQwertyKeyboard;
        }

这是什么意思?

我的测试结果是:

attribute.inputType = 17
EditorInfo.TYPE_MASK_CLASS = 15

它切换到 case EditorInfo.TYPE_CLASS_TEXT,即 = 1

最佳答案

二进制...

10001 // 17
01111 // 15

...& 运算符表示您要匹配 bits,其中两者都是 1

按位看起来像这样......

10001 // 17
01111 // 15
-----
00001 // 17 & 15

只有最后一个 bit 两个数字匹配为 1

关于android - 用 '&' 切换两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12343436/

相关文章:

android - 检查设备是否可以调用电话

java - 正确使用 Subscriber.onStart

java - 如何将我的状态机转换为 java?

java - 标记方法并通过标记从客户端对象调用它们

android - 如何从具有 singleInstance 启动模式的 Activity 中正确启动语音识别 Activity ?

android - 选项卡小部件中的背景忽略缩放

android - 在android中实现搜索

javascript - Vue.js Switch 语句未分配变量

javascript - 从外部访问 JavaScript switch 语句内声明的变量

javascript - 在javascript中使用switch语句显示图片