开发者

How to change password field to diplay asterisks instead of dots

I am working on the task that requires the password field (i.e.the Edit Text) to hide user input using asterisks(*) r开发者_StackOverflowather than dots(.). Currently it shows as dots. Kindly tell me the way to do it if its possible using android's native methods. Or please post the code to do it if anyone has already done that.

Thanks in advance..


Very late answer, and I'm sure you don't care anymore, but someone else might.

Initialize EditText Field .

 EditText UPL =(EditText) findViewById(R.id.UserPasswordToLogin) ;
    UPL.setTransformationMethod(new AsteriskPasswordTransformationMethod());

Then Create a new java class ,Called AsteriskPasswordTransformationMethod.java Which extends PasswordTransformationMethod

Here is code :

import android.text.method.PasswordTransformationMethod;
import android.view.View;

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


public final void setTransformationMethod (TransformationMethod method)

Since: API Level 1
Sets the transformation that is applied to the text that this TextView is displaying.
Related XML Attributes

android:password
android:singleLine

allows you to change any char


I would imagine you could override the listener class methods to modify the text to display so that it reads as "*", but keep the actual string in the background somewhere. So each time the user enters a letter, you add it to your cumulative "password" string, and instead, replace that character in the displayed string with *

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜