简体   繁体   中英

change edittext password characters to string “password is typing…”

I want to change password mask to string. I has used this code but this changes all of characters to one of p or a or s.

public class MyPasswordTransformationMethod extends PasswordTransformationMethod {

  @Override
  public CharSequence getTransformation(CharSequence source, View view) {
    return new PasswordCharSequence(source);
  }

  private class PasswordCharSequence implements CharSequence {

    private CharSequence mSource;

    public PasswordCharSequence(CharSequence source) {
      mSource = source;
    }

    public char charAt(int index) {
      char[] chars= new char[3];
      chars[0]='p';
      chars[1]='a';
      chars[2]='s';
      return chars[length()] ;


    }

    public int length() {
      return mSource.length();
    }

    public CharSequence subSequence(int start, int end) {
      return mSource.subSequence(start, end); // Return default
    }
  }
}
public char charAt(int index) {        }

returns one char at specified position (index)

so you need

return chars[index] ;

and you should, at least, add check if index is inside chars array borders.

Also you can simply write a switch, that will not create in-memory array for each function call:

public char charAt(int index) {
  switch (index) {
        case 0:  return 'p'; 
        case 1:  return 'a'; 
        case 2:  return 's'; 
        case 3:  return 's'; 
        ...
        default: return ' '; 
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM