如何将密码字段更改为显示星号而不是点

问题描述

| 我正在执行需要密码字段(即``编辑文本'')以使用星号(*)而不是点(。)隐藏用户输入的任务。当前它显示为点。 请告诉我如何使用android的本机方法来做到这一点。或者,如果任何人已经做过,请发布代码来完成。 提前致谢..     

解决方法

答案很晚了,我确定您已经不在乎了,但是其他人可能会再说。 初始化“ 0”字段。
 EditText UPL =(EditText) findViewById(R.id.UserPasswordToLogin) ;
    UPL.setTransformationMethod(new AsteriskPasswordTransformationMethod());
然后创建一个新的java类,称为
AsteriskPasswordTransformationMethod.java
,它扩展了PasswordTransformationMethod 这是代码:
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
允许您更改任何字符     ,我想您可以重写侦听器类的方法来修改要显示的文本,使其读为\“ * \”,但将实际的字符串保留在背景中的某个位置。因此,每次用户输入字母时,都将其添加到您的累积\“ password \”字符串中,而将显示的字符串中的该字符替换为*     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...