hpi*_*que 251 passwords fonts android android-edittext
当EditText处于密码模式时,似乎提示以不同的字体显示(courrier?).我怎么能避免这个?我希望提示以与EditText不在密码模式时相同的字体显示.
我目前的xml:
<EditText
android:hint="@string/edt_password_hint"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
android:singleLine="true" />
Run Code Online (Sandbox Code Playgroud)
man*_*sha 387
更改xml中的字体也不适用于我的提示文本.我找到了两种不同的解决方案,其中第二种解决方案对我有更好的行为:
1)android:inputType="textPassword"从你的xml文件中删除,而不是在java中设置它:
EditText password = (EditText) findViewById(R.id.password_text);
password.setTransformationMethod(new PasswordTransformationMethod());
Run Code Online (Sandbox Code Playgroud)
使用这种方法,提示字体看起来不错,但是当您在该编辑字段中键入时,在转换为密码点之前,您不会看到纯文本中的每个字符.此外,在全屏输入时,不会出现点,而是明文中的passoword.
2)留android:inputType="textPassword"在你的xml中.在Java中,ALSO设置了字体和passwordMethod:
EditText password = (EditText) findViewById(R.id.register_password_text);
password.setTypeface(Typeface.DEFAULT);
password.setTransformationMethod(new PasswordTransformationMethod());
Run Code Online (Sandbox Code Playgroud)
这种方法给了我想要的提示字体,并通过密码点给出了我想要的行为.
希望有所帮助!
ays*_*nje 192
我在Dialogs Guide中找到了这个有用的提示
提示:默认情况下,当您将EditText元素设置为使用"textPassword"输入类型时,字体系列将设置为等宽字体,因此您应将其字体系列更改为"sans-serif",以便两个文本字段都使用匹配的字体样式.
例如
android:fontFamily="sans-serif"
Run Code Online (Sandbox Code Playgroud)
Jam*_*ken 32
这就是我为解决这个问题所做的工作.出于某种原因,我没有必要设置转换方法,所以这可能是一个更好的解决方案:
在我的xml中:
<EditText
android:id="@+id/password_edit_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
Run Code Online (Sandbox Code Playgroud)
在我的Activity:
EditText password = (EditText) findViewById( R.id.password_edit_field );
password.setTypeface( Typeface.DEFAULT );
Run Code Online (Sandbox Code Playgroud)
rjr*_*rjr 21
setTransformationMethod方法为我打破了android:imeOption,并允许在密码字段中输入回车符.相反,我这样做:
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
setTypeface(Typeface.DEFAULT);
Run Code Online (Sandbox Code Playgroud)
而且我没有在XML中设置android:password ="true".
提供的答案manisha确实有效,但与默认状态相比,它使密码字段处于非标准状态.也就是说,默认字体也适用于密码字段,包括点替换和在用点替换之前出现的预览字符(以及当它是"可见密码"字段时).
要解决此问题并使其1)看起来和行为完全类似于默认textPassword输入类型,而且2)允许提示文本以默认(非等宽)字体显示,您需要TextWatcher在字段上有一个可以切换的字段fontface在它们之间来回正确,Typeface.DEFAULT并Typeface.MONOSPACE根据它是否为空.我创建了一个可用于实现此目的的辅助类:
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
/**
* This class watches the text input in a password field in order to toggle the field's font so that the hint text
* appears in a normal font and the password appears as monospace.
*
* <p />
* Works around an issue with the Hint typeface.
*
* @author jhansche
* @see <a
* href="http://stackoverflow.com/questions/3406534/password-hint-font-in-android">http://stackoverflow.com/questions/3406534/password-hint-font-in-android</a>
*/
public class PasswordFontfaceWatcher implements TextWatcher {
private static final int TEXT_VARIATION_PASSWORD =
(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
private TextView mView;
/**
* Register a new watcher for this {@code TextView} to alter the fontface based on the field's contents.
*
* <p />
* This is only necessary for a textPassword field that has a non-empty hint text. A view not meeting these
* conditions will incur no side effects.
*
* @param view
*/
public static void register(TextView view) {
final CharSequence hint = view.getHint();
final int inputType = view.getInputType();
final boolean isPassword = ((inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION))
== TEXT_VARIATION_PASSWORD);
if (isPassword && hint != null && !"".equals(hint)) {
PasswordFontfaceWatcher obj = new PasswordFontfaceWatcher(view);
view.addTextChangedListener(obj);
if (view.length() > 0) {
obj.setMonospaceFont();
} else {
obj.setDefaultFont();
}
}
}
public PasswordFontfaceWatcher(TextView view) {
mView = view;
}
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
// Not needed
}
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
if (s.length() == 0 && after > 0) {
// Input field went from empty to non-empty
setMonospaceFont();
}
}
public void afterTextChanged(final Editable s) {
if (s.length() == 0) {
// Input field went from non-empty to empty
setDefaultFont();
}
}
public void setDefaultFont() {
mView.setTypeface(Typeface.DEFAULT);
}
public void setMonospaceFont() {
mView.setTypeface(Typeface.MONOSPACE);
}
}
Run Code Online (Sandbox Code Playgroud)
然后要使用它,您需要做的就是调用register(View)静态方法.其他所有内容都是自动的(如果视图不需要,可以跳过变通方法!):
final EditText txtPassword = (EditText) view.findViewById(R.id.txt_password);
PasswordFontfaceWatcher.register(txtPassword);
Run Code Online (Sandbox Code Playgroud)
解决此问题的方法有很多,但每种方法各有利弊。这是我的测试
通过以下方式启用输入密码后,我只会在某些设备(答案末尾列出)中遇到此字体问题
edtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
Run Code Online (Sandbox Code Playgroud)
如果我使用android:inputType="textPassword",不会发生此问题
1)setTransformationMethod改用inputType
edtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
Run Code Online (Sandbox Code Playgroud)
2)使用 Typeface.DEFAULT
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
setTypeface(Typeface.DEFAULT);
Run Code Online (Sandbox Code Playgroud)
sans-serif-light是View我的应用程序中所有字体的默认字体=>之后setTypeface(Typeface.DEFAULT),该EditText字体在某些设备中仍然看起来有所不同3)使用 android:fontFamily="sans-serif"
在
setInputType重新使用字体之前先对其进行缓存
Typeface cache = edtPassword.getTypeface();
edtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
edtPassword.setTypeface(cache);
Run Code Online (Sandbox Code Playgroud)
测试
某些设备字体字体问题
某些设备没有字体问题
| 归档时间: |
|
| 查看次数: |
62422 次 |
| 最近记录: |