在隐藏之前显示密码字符一小段时间

Show password characters for a short while before hiding them

在我的应用程序中,我实现了自己的数字键盘,用于输入(基于数字的)密码。 'password',或者更确切地说是 'dot' 的表示,显示在数字键盘上方 4 个框中实现的 4 个 EditText 上。当你输入一个数字时,其中一个框将用一个点填充 - 为此我使用 TransformationMethod PasswordTransformationMethod - 但我希望你输入的数字在它被隐藏之前短时间内可见(比如〜200ms ).

在 Android 中是否有任何易于完成的实现,或者您将如何以最佳方式实现?我相信您知道我的意思,它经常用于基于移动设备的密码输入。

编辑:提示后的代码:

codeField1 = (EditText) findViewById(R.id.passcode_1);
codeField2 = (EditText) findViewById(R.id.passcode_2);
codeField3 = (EditText) findViewById(R.id.passcode_3);
codeField4 = (EditText) findViewById(R.id.passcode_4);

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            codeField1.setTransformationMethod(PasswordTransformationMethod.getInstance());
            codeField2.setTransformationMethod(PasswordTransformationMethod.getInstance());
            codeField3.setTransformationMethod(PasswordTransformationMethod.getInstance());
            codeField4.setTransformationMethod(PasswordTransformationMethod.getInstance());
       }
    }, 2000);

仍然给我问题,只影响我输入的第一个..(所有这些都在 onCreate() 中)。

使用TextWatcher

不要在 edittext.Instead 中设置 android:password="true" 属性 使用 textwatcher 并在 addTextChangedListener() 的 textwatcher 在等待 200 毫秒后用 * 替换密码。

等待使用以下代码:

private Handler mHandler = new Handler();
 mHandler.postDelayed(new Runnable() {
            public void run() {
                doStuff();
            }
        }, 2000);

试试这个代码..

这将在 2 秒后隐藏文本,您可以根据需要更改它..

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }, 2000);  // set time based on your requirement