如何在安卓系统中增加密码输入框中黑点的大小

13

我有一个EditText,其inputTypenumberPassword。我想要更改替换文本的点的大小。对于我的目的来说,它(Android 默认值)相当小。我想增大点的大小。如何实现?


这就像用星号替换点号一样。@ADM - sagar suri
没错!这只是为了参考,以便得到一些想法。我并没有说这就是答案。 - ADM
4个回答

13

尝试用这些 ASCII 码替换星号:
⚫ - ⚫ - 中等大小的黑色圆圈
⬤ - &#11044 - 大黑色圆圈

如何在字符中间插入大圆点符号的 Unicode 字符是什么?

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

text.setTransformationMethod(new MyPasswordTransformationMethod());

9
解决方案只能部分地解决问题,问题在于该转换会将文本直接转换为'*',而期望的行为是在输入新字符或几秒钟后隐藏字符,以便用户有机会在隐藏之前看到真正的字符。如果您想保持默认行为,应该像这样做:
/**
 * A transformation to increase the size of the dots displayed in the text.
 */
private object BiggerDotPasswordTransformationMethod : PasswordTransformationMethod() {

    override fun getTransformation(source: CharSequence, view: View): CharSequence {
        return PasswordCharSequence(super.getTransformation(source, view))
    }

    private class PasswordCharSequence(
        val transformation: CharSequence
    ) : CharSequence by transformation {
        override fun get(index: Int): Char = if (transformation[index] == DOT) {
            BIGGER_DOT
        } else {
            transformation[index]
        }
    }

    private const val DOT = '\u2022'
    private const val BIGGER_DOT = '●'
}

这将使用任何您想要的字符替换原始点。

1

Kotlin中更加简单。您不需要创建一个单独的类来覆盖单个方法。在Kotlin中,您可以这样做:

val passwordTransformation =  object: PasswordTransformationMethod() {
                    override fun getTransformation(
                        source: CharSequence?,
                        view: View?
                    ): CharSequence {
                        return super.getTransformation(source, view).replace(Regex("\u2022"), "●")
                    }
                }

使用方法如下:

textView?.transformationMethod = passwordTransformation

0

尝试使用这个

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/etPasswordLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:passwordToggleEnabled="true">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:text="password"
                android:padding="0dp"
                android:textSize="40dp"
                android:background="@color/white"
                android:inputType="textPassword" />

        </com.google.android.material.textfield.TextInputLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接