如何在 android 中增加 inputType 密码点的大小
How to increase inputType password dot size in android
我有一个 EditText
,其 inputType
是 numberPassword
。我想更改替换文本的点大小。就我的目的而言,它很小(android 默认值)。我想增加点的大小。我怎样才能做到这一点?
尝试用这个 ascii 码替换星号。
⚫ - ⚫ - 中号黑圈
⬤ - ⬤ - 黑色大圆圈
What would be the Unicode character for big bullet in the middle of the character?
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());
提供的解决方案只奏效了一半,问题是转换会将文本直接转换为“*”,而预期的行为是在输入新字符后或几秒钟后隐藏字符,因此用户有机会在隐藏之前看到真正的字符。如果你想保持默认行为,你应该这样做:
/**
* 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 = '●'
}
这会将原始点替换为您想要的任何字符。
试试这个
<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>
kotlin 中要简单得多。您不需要创建单独的 class 来覆盖单个方法。在科特林中,你可以这样做:
val passwordTransformation = object: PasswordTransformationMethod() {
override fun getTransformation(
source: CharSequence?,
view: View?
): CharSequence {
return super.getTransformation(source, view).replace(Regex("\u2022"), "●")
}
}
这样使用:
textView?.transformationMethod = passwordTransformation
我有一个 EditText
,其 inputType
是 numberPassword
。我想更改替换文本的点大小。就我的目的而言,它很小(android 默认值)。我想增加点的大小。我怎样才能做到这一点?
尝试用这个 ascii 码替换星号。
⚫ - ⚫ - 中号黑圈
⬤ - ⬤ - 黑色大圆圈
What would be the Unicode character for big bullet in the middle of the character?
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());
提供的解决方案只奏效了一半,问题是转换会将文本直接转换为“*”,而预期的行为是在输入新字符后或几秒钟后隐藏字符,因此用户有机会在隐藏之前看到真正的字符。如果你想保持默认行为,你应该这样做:
/**
* 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 = '●'
}
这会将原始点替换为您想要的任何字符。
试试这个
<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>
kotlin 中要简单得多。您不需要创建单独的 class 来覆盖单个方法。在科特林中,你可以这样做:
val passwordTransformation = object: PasswordTransformationMethod() {
override fun getTransformation(
source: CharSequence?,
view: View?
): CharSequence {
return super.getTransformation(source, view).replace(Regex("\u2022"), "●")
}
}
这样使用:
textView?.transformationMethod = passwordTransformation