[英]Android : Typeface is changed when i apply password Type on EditText
當用戶開始在 EditText Android 中編寫內容時,我使用FloatLabel庫 ( https://github.com/weddingparty/AndroidFloatLabel ) 添加一些動畫。
我的問題是當我將密碼類型應用於我的 EditText 時,字體似乎發生了變化。 我想保持與往常一樣的字體。 (見圖1)
但是當我添加以下行來應用密碼類型時,提示的字體似乎發生了變化!
pass.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
以下可能會為您解決。
pass.setTypeface(user.getTypeface());
本質上,它只是傳遞用戶Typeface
段的Typeface
並將其作為密碼字段的Typeface
傳遞。
我在Dialogs API Guide 中找到了對此的解釋。
提示:默認情況下,當您將 EditText 元素設置為使用
"textPassword"
輸入類型時,字體系列設置為等寬,因此您應該將其字體系列更改為"sans-serif"
以便兩個文本字段都使用匹配的字體風格。
換句話說,可以在 XML 中完成的修復如下:
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:hint="@string/password"/>
出於某種原因,我不必設置轉換方法,因此這可能是一個更好的解決方案。 在我的活動中:
EditText editText_password = findViewById(R.id.edittext);
editText_password.setTransformationMethod(new PasswordTransformationMethod());
您可以在您的活動中使用 android:hit 和 setHitColor,它不會影響正常輸入
在xml中將inputType設置為“text”,
<EditText
android:id="@+id/edtPassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="text"/>
然后將活動中的 EditText 設為
mEdtConfirmPassword = findViewById(R.id.edtPassword);
mEdtConfirmPassword.setTransformationMethod(new PasswordTransformationMethod());
回答這個問題可能為時已晚,但我遇到了這個問題。 所以想到回答這個。 我已經通過Implementing a CustomTextWatcher
解決了它。
這是片段
private class CustomTextWatcher implements TextWatcher {
private EditText mEditText;
public CustomTextWatcher(EditText e) {
mEditText = e;
mEditText.setTypeface(Typeface.DEFAULT);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
mEditText.setTypeface(Typeface.DEFAULT);
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
mEditText.setTypeface(Typeface.MONOSPACE);
}
public void afterTextChanged(Editable s) {
if(s.length() <= 0){
mEditText.setTypeface(Typeface.DEFAULT);
} else {
mEditText.setTypeface(Typeface.MONOSPACE);
}
}
}
以下是如何在您的應用程序中使用它
EditText pin = (EditText) findViewById(R.id.pin);
pin.addTextChangedListener(new CustomTextWatcher(pin));
另一種解決方案是在更改inputType
之前保存typeface
並在之后重新應用它。 奇怪的是,在xml
設置inputType
時不會發生這種情況,但在以編程方式設置時會發生這種情況。
val typeface = editText.typeface
editText.inputType = inputType
editText.typeface = typeface
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.