在android中如何显示星号(*)代替EditText中的点,输入类型为textPassword?

In android how to show asterisk (*) in place of dots in EditText having inputtype as textPassword?(在android中如何显示星号(*)代替EditText中的点,输入类型为textPassword?)
本文介绍了在android中如何显示星号(*)代替EditText中的点,输入类型为textPassword?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我试图用 asterisk (*) 符号代替 EditText 中的点,其中 inputTypetextPassword.我遇到了要求使用 setTransformationMethod() 并实现 PasswordTransformationMethod 的帖子.但是我需要实现该类的哪个方法以及如何显示星号?还有其他方法吗?

I am trying to show in asterisk (*) symbol in place of dots in EditText having inputType as textPassword. I came across post that ask to use setTransformationMethod() and implement PasswordTransformationMethod. But which method I of that class need I implement and how show asterisk? Is there other way to do that?

谢谢

推荐答案

我觉得你应该通过文档.创建你的 PasswordTransformationMethod 类,并在 getTransformation() 方法中,只返回与内容长度相同的 * 字符串您的密码字段.

I think you should go through the documentation. Create your PasswordTransformationMethod class, and in the getTransformation() method, just return a string of * characters that is the same length as the contents of your password field.

我做了一些摆弄,想出了一个匿名类,它可以让我创建一个充满 * 的字段.我在这里将其转换为可用的类:

I did some fiddling and came up with an anonymous class that worked for me to make a field full of *s. I converted it into a usable class here:

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

// Call the above class using this:
text.setTransformationMethod(new MyPasswordTransformationMethod());

这篇关于在android中如何显示星号(*)代替EditText中的点,输入类型为textPassword?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Prevent enter key on EditText but still show the text as multi-line(防止在 EditText 上输入键,但仍将文本显示为多行)
Android keyboard next button issue on EditText(EditText上的Android键盘下一个按钮问题)
When the soft keyboard appears, it makes my EditText field lose focus(当软键盘出现时,它使我的 EditText 字段失去焦点)
android how to make text in an edittext exactly fixed lines(android如何在edittext中制作文本完全固定的行)
How to use regular expression in Android(如何在 Android 中使用正则表达式)
Filter list view from edit text(从编辑文本中过滤列表视图)