我可以使用 numberPassword 输入类型并仍然看到输入吗?

Can I use numberPassword input type and still see the input?

我可以使用 numberPassword 作为输入类型并仍然看到输入吗?

我的目标是拥有一个 0-9 键盘和尽可能少的其他键(这就是为什么我更喜欢 numberPassword 而不是 phone),但用户仍然应该能够看到什么他刚打字。

这就是我所拥有的,但现在密码隐藏在星号后面。

android:digits="1234567890"
android:inputType="numberPassword"

注意:我也试过setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD),它使输入可见,但将键盘从纯数字键盘更改为常规键盘。不过我需要他们的键盘来显示数字。

在Java中,只需这样做:

edittext.setTransformationMethod(PasswordTransformationMethod.getInstance());

如果以后想隐藏密码,只需这样做:

editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

有多种方法可以解决这个问题

  1. 您可以将 TextInputEditText 与 TextInputLayout 结合使用,如下所示

    <android.support.design.widget.TextInputLayout
         android:id="@+id/etPasswordLayout"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         app:passwordToggleEnabled="true"
         android:layout_marginBottom="@dimen/login_spacing_bottom">
    
    <android.support.design.widget.TextInputEditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/fragment_login_password_hint"
        android:inputType="textPassword"/>
    </android.support.design.widget.TextInputLayout>
    

使用 passwordToggleEnabled,您可以在星号和密码值之间切换

  1. 您可以使用转换 class 来切换密码星号和值,如下所示

    edittextObject.setTransformationMethod(PasswordTransformationMethod.getInstance());
    
  2. 您可以使用要在键盘上显示的值创建自定义键盘。你可以在我写的关于 custom keyboard

  3. 的教程中看到如何实现这一点

这完成了工作:

uEditText = (EditText) findViewById(R.id.editText);
uEditText.setTransformationMethod(null);