Change EditText password mask character to asteris

2020-01-27 02:27发布

Is there any way to change the password text from dot(.) to asterisk(*) .

Password is entering in edittext.

<EditText
        android:id="@+id/passWord1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:inputType="number"
        android:password="true"/>

6条回答
The star\"
2楼-- · 2020-01-27 02:50

Insert edittext in your xml file,

<EditText
    android:id="@+id/passWordEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:inputType="textPassword"/>

and your class file go on and get findViewById from edittext and implement for this,

EditText edittext = (EditText)findViewById(R.id.passWordEditText);
edittext.setTransformationMethod(new AsteriskPasswordTransformationMethod());

and This class implement for that,

public class AsteriskPasswordTransformationMethod 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; // Store char sequence
        }
        public char charAt(int index) {
            return '*'; // This is the important part
        }
        public int length() {
            return mSource.length(); // Return default
        }
        public CharSequence subSequence(int start, int end) {
            return mSource.subSequence(start, end); // Return default
        }
    }
};
查看更多
虎瘦雄心在
3楼-- · 2020-01-27 02:53

Try this

android:inputType="textPassword"       
查看更多
啃猪蹄的小仙女
4楼-- · 2020-01-27 02:54

Got answer with help of link posted by Ram kiran

text.setTransformationMethod(new AsteriskPasswordTransformationMethod());


public class AsteriskPasswordTransformationMethod 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; // Store char sequence
    }
    public char charAt(int index) {
        return '*'; // This is the important part
    }
    public int length() {
        return mSource.length(); // Return default
    }
    public CharSequence subSequence(int start, int end) {
        return mSource.subSequence(start, end); // Return default
    }
}
};
查看更多
贪生不怕死
5楼-- · 2020-01-27 03:02
<EditText
        android:id="@+id/passWord1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:inputType="textPassword"//here is the change. check it once in your xml
        android:password="true"/>

In eclipse there will be hints when you click Ctrl + Space when you move cursor at android:inputType. Then you can see list of options. there you can select textPassword

If you want to see * in place of . then check this Android: Asterisk Password Field

查看更多
走好不送
6楼-- · 2020-01-27 03:02

For android:inputType, there is a type of password.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2020-01-27 03:05
EditText.setTransformationMethod(new PasswordTransformationMethod());
查看更多
登录 后发表回答