Problem with android password field, not hiding th

2020-03-03 05:21发布

问题:

In Android, I create a password field like this:

    EditText text = new EditText(context);
    text.setTransformationMethod(PasswordTransformationMethod.getInstance());

Or like this, which seems to do the same thing:

    EditText text = new EditText(context);
    text.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

I get a nice password field except for the last character typed by the user. It's visible on the screen for a few seconds before beeing masked with a dot.

Here is a screenshot:

Do you know how to fix this behaviour please?

回答1:

This is expected behavior. With the soft keyboards on most devices, it is valuable feedback that they are typing the password correctly.

For a list of all of the different inputTypes you can specify and how they change the EditText,

see android inputTypes .

Also, it is possible to change this behavior by implementing your own TransformationMethod and setting it via setTransformationMethod(), but I would not recommend doing that. Users will expect the behavior you are seeing and by changing your app, you'll be providing an inconsistent user experience.

also check this android article



回答2:

Implementation of TransformationMethod to hide last char typed in password:

public class LoginActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // example of usage
    ((TextView) findViewById(R.id.password)).setTransformationMethod(new HiddenPassTransformationMethod());
}

private class HiddenPassTransformationMethod implements TransformationMethod {

    private char DOT = '\u2022';

    @Override
    public CharSequence getTransformation(final CharSequence charSequence, final View view) {
        return new PassCharSequence(charSequence);
    }

    @Override
    public void onFocusChanged(final View view, final CharSequence charSequence, final boolean b, final int i,
                               final Rect rect) {
        //nothing to do here
    }

    private class PassCharSequence implements CharSequence {

        private final CharSequence charSequence;

        public PassCharSequence(final CharSequence charSequence) {
            this.charSequence = charSequence;
        }

        @Override
        public char charAt(final int index) {
            return DOT;
        }

        @Override
        public int length() {
            return charSequence.length();
        }

        @Override
        public CharSequence subSequence(final int start, final int end) {
            return new PassCharSequence(charSequence.subSequence(start, end));
        }
    }
}
}


回答3:

I came across this problem after needing to implement a pin-code type user interface. After the user entered 1 number (1:1 relationship between EditTexts and pin numbers for a total of 4 EditTexts) the password would remain "in the open". My solution was to implement a TextWatcher that replaced the input with bullets (•'s).

See the full answer here