Show password characters for a short while before

2019-05-27 03:39发布

问题:

In my application I've implemented a num-pad of my own, which is used for entering (number-based) passcodes. The 'password', or rather the 'dot'-representation of it, is shown on 4 EditTexts implemented in 4 boxes above the num-pad. When you enter a number one of those boxes will get filled in with a dot - for this I use the TransformationMethod PasswordTransformationMethod - but I'd want the number you punch in to be visible for a short while before it gets hidden (Like ~200ms).

Is there any implementation of this readily-done in Android or how would you implement this the best way? I'm sure you know what I mean, it's used quite frequently for mobile-based password entering.

Edit: Code after tips:

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);

Still giving me issues, only effect on the first one I enter.. (All this is in onCreate()).

回答1:

Try this code ..

this will hide the text after 2 second and you can change it based on your requi..

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


回答2:

Use TextWatcher

Do not set the android:password="true" property in edittext.Instead use a textwatcher and in the addTextChangedListener() of the textwatcher replace the password with * after waiting for 200ms.

To wait use the following code:

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