基本的Andr​​oid code - EditText上的inputType下 - 为什么在位运算符使用吗? [英] Basic Android code - EditText's InputType - why was the bitwise operator used?

查看:198
本文介绍了基本的Andr​​oid code - EditText上的inputType下 - 为什么在位运算符使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一堆Android的教程的工作我的方式在YouTube上,我希望他们有点过时了现在。我对目前的位具有以下code:

I'm working my way through a bunch of Android tutorials on YouTube, I expect they are a bit out of date by now. The bit I'm on currently has the following code:

tglButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if (tglButton.isChecked()) {
            inputText.setInputType(InputType.TYPE_CLASS_TEXT | 
                                   InputType.TYPE_TEXT_VARIATION_PASSWORD);
        } else {
            inputText.setInputType(InputType.TYPE_CLASS_TEXT);
        }
    }
});

过为什么按位操作者用在这里,顶评论说,该视频釉料:

The video glazes over why the bitwise operator is used here, and the top comment says that:

每个InputTypes的实际上只是整数。 TYPE_CLASS_TEXT是1,
  和TYPE_TEXT_VARIATION_PASSWORD为128(或10000000)。

Each of the InputTypes are actually just ints. TYPE_CLASS_TEXT is 1, and TYPE_TEXT_VARIATION_PASSWORD is 128 (or 10000000).

执行按位或对他们的:

00000001

千万

10000001

这是129

尝试输入input.setInputType(129);相反,你会看到它会工作。 :)

Try entering input.setInputType(129); instead, you'll see it'll work. :)

为什么呢?这一部分的目的是要切换的EditText 文本密码类型视的切换按钮的状态。为什么是129,而不是128,为什么呢,对于这个问题是它甚至使用。

Why? The aim of this part is to toggle the type of the EditText from text to password depending on the state of the ToggleButton. Why is it 129, and not 128, and why, for that matter is it even used.

我敢肯定有更好的方式来实现这一目标;但我希望能理解为什么它已经做了这种方式。

I'm sure there's a better way to achieve this; but I am hoping to understand why it has been done this way.

推荐答案

这类型的构造是在编程中常见的。这些类型整数有时称为二进制标志。标志可以组合和二元运算哪些计算机可以很快做到快速测试。如果一个对象(这里是一个EditText),可以执行不同的方式取决于几个设置,分配使用二进制标志这些设置可以是有效的。在Java中的整数是32位,因此,如果标志被组合的32个人标志可支撑并有更多的值。这里所述的EditText被告知要么只有一个文本字段(TYPE_CLASS_TEXT为二进制1,这也是整数1)或通过用TYPE_TEXT_VARIATION_PASSWORD标记它(二进制10000000或其整数128)成为一个密码字段。二元或在值结果10000001,整​​数129所以129再presents密码文本字段(即,它被标记为文本和密码)。如果这是一个OR与TYPE_TEXT_VARIATION_EMAIL_ADDRESS TYPE_CLASS_TEXT(二进制100000,整数32),其结果将是100001,整​​数33,所以33重新presents的文本字段标记检查的电子邮件地址。

This type of construct is common in programming. These type of integers are sometimes referred to as binary flags. Flags can be combined and tested quickly with binary operations which computers can do very quickly. If an object (here an EditText) can perform differently depending on several settings, assigning those settings using binary flags can be efficient. An integer in Java is 32 bits, so 32 individual flags can be supported and many more values if flags are combined. Here the EditText is being told either to be a text field only (TYPE_CLASS_TEXT is binary 1, which is also integer 1) or to become a password field by flagging it with TYPE_TEXT_VARIATION_PASSWORD (binary 10000000 or integer 128). The binary OR on the values results in 10000001, integer 129. So 129 represents a password text field (i.e. it is flagged as text and password). If it was an OR on TYPE_CLASS_TEXT with TYPE_TEXT_VARIATION_EMAIL_ADDRESS (binary 100000, integer 32) the result would be 100001, integer 33, so 33 represents a text field flagged to check for an email address.

这篇关于基本的Andr​​oid code - EditText上的inputType下 - 为什么在位运算符使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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