在具有 inputType=textPassword 的 EditText 上调用 setSingleLine 使字符可见
Calling setSingleLine on an EditText with inputType=textPassword make characters visible
我在 activity 的 XML
中添加了带有 inputType="textPassword"
的 EditText
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword"
android:padding="16dp"
android:id="@+id/passwordInput"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
到目前为止没有任何问题,我看到的是圆圈而不是真正的密码字符:
有趣的部分来了。现在,如果我在 Activity:
中的 EditText 上调用 setSingleLine()
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
passwordInput.setSingleLine()
}
}
会发现密码字符出奇地明显!
另一个有趣的是,如果我把 android:singleLine="true"
放在 EditText 的 XML 中,就不会发生这个问题。
注意:我知道在密码字段上设置setSingleLine
是没有用的,但我很好奇为什么调用这个函数会有这样的副作用。
尝试在里面设置XML:
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
我不能告诉你为什么以编程方式设置它会导致这种奇怪的行为,因为我通常在 XML
中这样做,而我在代码中这样做只是为了操纵 Views
。
如果你想隐藏密码,你会:
yourTextView.setTransformationMethod(新密码转换方法());
如果你想显示密码,你会:
yourTextView.setTransformationMethod(new DoNothingTransformation()),或 setTransformationMethod(null)
方法setTransformationMethod是show/hide文本
现在可以查看classTextView的代码,因为EditText是从TextView扩展出来的。
你会看到在函数setSingleLine()中,是调用函数applySingleLine(singleLine, true, true),这个函数会再次设置setTransformationMethod(SingleLineTransformationMethod.getInstance());
这是更改您的转换(show/hide EditText 的文本):
private void applySingleLine(boolean singleLine, boolean applyTransformation,
boolean changeMaxLines) {
mSingleLine = singleLine;
if (singleLine) {
setLines(1);
setHorizontallyScrolling(true);
// change Transformation
if (applyTransformation) {
setTransformationMethod(SingleLineTransformationMethod.getInstance());
}
} else {
if (changeMaxLines) {
setMaxLines(Integer.MAX_VALUE);
}
setHorizontallyScrolling(false);
if (applyTransformation) {
setTransformationMethod(null);
}
}
}
我想是因为当你调用setSingleLine
时,textview会将它的转换方法从PasswordTransformationMethod
更改为SingleLineTransformationMethod
。 EditText
(即TextView
的child)
当时只接受一种转换方法
你可以在这里查看源代码:
setSingleLine()
https://android.googlesource.com/platform/frameworks/base/+/jb-mr0-release/core/java/android/widget/TextView.java#6727
按照代码填充调用函数setTransformationMethod
我在 activity 的 XML
中添加了带有inputType="textPassword"
的 EditText
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword"
android:padding="16dp"
android:id="@+id/passwordInput"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
到目前为止没有任何问题,我看到的是圆圈而不是真正的密码字符:
有趣的部分来了。现在,如果我在 Activity:
中的 EditText 上调用setSingleLine()
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
passwordInput.setSingleLine()
}
}
会发现密码字符出奇地明显!
另一个有趣的是,如果我把 android:singleLine="true"
放在 EditText 的 XML 中,就不会发生这个问题。
注意:我知道在密码字段上设置setSingleLine
是没有用的,但我很好奇为什么调用这个函数会有这样的副作用。
尝试在里面设置XML:
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
我不能告诉你为什么以编程方式设置它会导致这种奇怪的行为,因为我通常在 XML
中这样做,而我在代码中这样做只是为了操纵 Views
。
如果你想隐藏密码,你会:
yourTextView.setTransformationMethod(新密码转换方法());
如果你想显示密码,你会:
yourTextView.setTransformationMethod(new DoNothingTransformation()),或 setTransformationMethod(null)
方法setTransformationMethod是show/hide文本
现在可以查看classTextView的代码,因为EditText是从TextView扩展出来的。 你会看到在函数setSingleLine()中,是调用函数applySingleLine(singleLine, true, true),这个函数会再次设置setTransformationMethod(SingleLineTransformationMethod.getInstance()); 这是更改您的转换(show/hide EditText 的文本):
private void applySingleLine(boolean singleLine, boolean applyTransformation, boolean changeMaxLines) { mSingleLine = singleLine; if (singleLine) { setLines(1); setHorizontallyScrolling(true); // change Transformation if (applyTransformation) { setTransformationMethod(SingleLineTransformationMethod.getInstance()); } } else { if (changeMaxLines) { setMaxLines(Integer.MAX_VALUE); } setHorizontallyScrolling(false); if (applyTransformation) { setTransformationMethod(null); } }
}
我想是因为当你调用setSingleLine
时,textview会将它的转换方法从PasswordTransformationMethod
更改为SingleLineTransformationMethod
。 EditText
(即TextView
的child)
你可以在这里查看源代码:
setSingleLine()
https://android.googlesource.com/platform/frameworks/base/+/jb-mr0-release/core/java/android/widget/TextView.java#6727
按照代码填充调用函数setTransformationMethod