【问题标题】:Unable to use numberPassword as input type无法使用 numberPassword 作为输入类型
【发布时间】:2014-08-14 09:07:25
【问题描述】:

我正在尝试使用 android:inputType="numberPassword" 但它不起作用。数字仍然出现在 EditText 中。仅当我在输入第一个后添加 TextWatcher 以切换到下一个 EditText 时才会发生这种情况。

public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        if (ps1.getText().hashCode() == s.hashCode()) {
            ps1.clearFocus();
            ps2.requestFocus();
            ps2.setCursorVisible(true);
        } else if (ps2.getText().hashCode() == s.hashCode()) {
            ps2.clearFocus();
            ps3.requestFocus();
            ps3.setCursorVisible(true);
        } else if (ps3.getText().hashCode() == s.hashCode()) {
            ps3.clearFocus();
            ps4.requestFocus();
            ps4.setCursorVisible(true);
        }
    }

我可以做些什么来使它正常工作?

【问题讨论】:

    标签: android textwatcher


    【解决方案1】:

    只需在您的 XML 中编写您的 Edit-texts,如下所示。

    <EditText
                    android:id="@+id/ed_login_password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:inputType="textPassword"
                    android:singleLine="true"
                    android:text=""
    />
    

    尝试以编程方式 setinputtypeEdittext

    editPass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)
    

    我认为它们是您的 Textwatcher 中的一个问题,因此如果您想将 单个 TextWatcher 用于多个 EditTexts

    查看以下链接:-How to use Single TextWatcher for multiple EditTexts?

    通过TextWatcher的以下方法访问您的Editexts

    public void afterTextChanged(Editable editable) {
    
     }
    

    希望对你有帮助。

    【讨论】:

    • 谢谢。并不是我认为,使用相同的 TextWatcher 可能是您所说的问题。我会检查并告诉它进展如何。
    • 这些是我们提供的唯一可能的解决方案。我认为它们仅此而已。
    • 我已经提供了答案。请看一下。
    • 您能否查看我提供的链接,它会和您现在一样。我告诉您,它们将在您的 Textwatcher 中出错。
    【解决方案2】:
    This problem can be solved without using deprecated android:password. Use the following code snippet, but do not reverse the sequence of calls:
    
        EditText editText = (EditText) findViewById(R.id.MyEditText);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
    

    【讨论】:

    • 试试这个:
    • 你能告诉我有什么改变可以使这项工作奏效吗?
    【解决方案3】:

    在你的 xml 文件中使用下面的行希望它会帮助你

    android:password="true"
    

    可能它已被弃用,但只要尝试一下就能解决您的问题

    或者你可以试试

    android:inputType="textPassword|number" 
    

    忽略textPassword,只接受数字不接受密码。

    所以你应该使用android:inputType="textPassword|number"android:password="true".

    这似乎是唯一的解决方案。

    【讨论】:

    • 不工作。有四个编辑文本。第一个和最后一个被更改,但第二个和第三个保持为数字。
    【解决方案4】:

    如果有人正在寻找答案,这很简单。让您的 EditText 有这一行 android:inputType="numberPassword"。不需要使用折旧的东西。只需将代码放在 afterTextChanged 而不是 onTextChanged 中即可。

     @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            if (ps1.getText().hashCode() == s.hashCode()) {
                ps1.clearFocus();
                ps2.requestFocus();
                ps2.setCursorVisible(true);
            } else if (ps2.getText().hashCode() == s.hashCode()) {
                ps2.clearFocus();
                ps3.requestFocus();
                ps3.setCursorVisible(true);
            } else if (ps3.getText().hashCode() == s.hashCode()) {
                ps3.clearFocus();
                ps4.requestFocus();
                ps4.setCursorVisible(true);
            }
        }
    

    我不确定为什么会这样,所以请知道的人解释一下。

    【讨论】:

      【解决方案5】:

      对我来说 android:inputType="numberPassword" 适用于 验证编辑文本

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-21
        • 2015-05-14
        • 2019-06-10
        • 2020-09-05
        • 1970-01-01
        • 2020-03-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多