开发者

Problem with android password field, not hiding the last character typed

开发者 https://www.devze.com 2023-03-13 16:34 出处:网络
In Android, I create a password field like this: EditText text = new EditText(context); text.setTransformationMethod(PasswordTransformationMethod.getInstance());

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:

    Edit开发者_StackOverflow中文版Text 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:

Problem with android password field, not hiding the last character typed

Do you know how to fix this behaviour please?


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


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


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


I was also facing same issues, in my case there was 3 edit text each edit text can have maximum 4 digit number for Aadhaar number. I used numberPassword input type.

When user type Aadhaar number very fast first edit text last text 4th was not hiding because I am changing focus when 4 digit completed. I have added text watcher.

I got solution at the end of last edit text when 4 digit completed I requestFocus for all 3 edit text one by one.

Problem with android password field, not hiding the last character typed

Solution :

  @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        if (getBinding().edtFirstFourNumber.getText().toString().length() == 4 && getBinding().edtMiddleFourNumber.getText().toString().length() == 4 && getBinding().edtLastFourNumber.getText().toString().length() == 4) {
            //  getBinding().homeProceed.setAlpha(1f);
            getBinding().homeProceed.setTextColor(ContextCompat.getColor(mContext, R.color.color_ffffff));
            getBinding().homeProceed.setEnabled(true);
        } else {
            //  getBinding().homeProceed.setAlpha(0.5f);
            getBinding().homeProceed.setTextColor(ContextCompat.getColor(mContext, R.color.color_4090BE));
            getBinding().homeProceed.setEnabled(false);
        }
        if (getBinding().edtFirstFourNumber.length() == 4) {
            getBinding().edtMiddleFourNumber.requestFocus();
        }
        if (getBinding().edtMiddleFourNumber.length() == 4) {
            getBinding().edtLastFourNumber.requestFocus();
        }
        if (getBinding().edtLastFourNumber.length() == 4) {
            getBinding().edtFirstFourNumber.requestFocus();
            getBinding().edtMiddleFourNumber.requestFocus();
            getBinding().edtLastFourNumber.requestFocus();
            CommonUtils.hideSoftKeyboard(getActivity());
        }

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消