我有一个EditText
who inputType
is numberPassword
。我想更改替换文本的点大小。就我的目的而言,它很小(android 默认)。我想增加点的大小。我怎样才能做到这一点?
问问题
6309 次
4 回答
13
尝试用这个 ascii 代码替换星号。
⚫ - ⚫ - 中等黑色圆圈 ⬤ - ⬤ - 黑色大圆圈
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());
于 2018-01-09T07:27:09.107 回答
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 = '●'
}
这将用您想要的任何字符替换原始点。
于 2018-12-03T05:30:25.570 回答
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>
于 2020-12-15T05:40:40.827 回答
0
在 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
于 2022-01-28T04:54:48.493 回答