当EditText处于密码模式时,提示似乎以不同的字体显示(courier ?)我该如何避免这种情况?我想提示出现在相同的字体,当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" />

当前回答

我使用这个解决方案来切换字体取决于提示可见性。它与Joe的答案类似,但扩展了EditText:

public class PasswordEditText extends android.support.v7.widget.AppCompatEditText {

    public PasswordEditText(Context context) {
        super(context);
    }

    public PasswordEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PasswordEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        if (text.length() > 0) setTypeface(Typeface.MONOSPACE);
        else setTypeface(Typeface.DEFAULT);
    }

}

其他回答

这是如何使输入密码有提示,不转换为*和默认字体!!

关于XML:

android:inputType="textPassword"
android:gravity="center"
android:ellipsize="start"
android:hint="Input Password !."

活动介绍:

inputPassword.setTypeface(Typeface.DEFAULT);

感谢芒果和rjjr的洞察力:D。

也许是一个奇怪的例子,但我已经做过实验,并发现:

password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
password.setTransformationMethod(new PasswordTransformationMethod());

改变了提示字体的大小,而不是字体本身!这仍然是一种不受欢迎的效果。奇怪的是,相反的操作:

password.setTransformationMethod(new PasswordTransformationMethod());
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

保持相同的字体大小。

使用书法图书馆。

然后它仍然不会用正确的字体更新密码字段。所以在代码中,而不是在xml中:

Typeface typeface_temp = editText.getTypeface();
editText.setInputType(inputType); /*whatever inputType you want like "TYPE_TEXT_FLAG_NO_SUGGESTIONS"*/
//font is now messed up ..set it back with the below call
editText.setTypeface(typeface_temp); 

我知道这可能是旧的一个,但当我一起使用InputTypeand app:passwordToggleEnabled="true"时,我遇到了与此问题相关的问题。

写这个,可能会对在座的人有所帮助。

我想使用自定义字体密码字段以及app:passwordToggleEnabled选项为我的密码输入字段。但是在27.1.1支持库中(在撰写本文时),它崩溃了。

代码是这样的,

<android.support.design.widget.TextInputLayout
        android:id="@+id/input_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/_10dp"
        android:layout_marginTop="@dimen/_32dp"
        android:hint="@string/current_password"
        android:textColorHint="@color/hint_text_color"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:passwordToggleEnabled="true"
        app:passwordToggleTint="@color/black">


        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="start|left"
            android:maxLines="1"
            android:textAlignment="viewStart"
            android:textColor="@color/black"
            android:textColorHint="@color/camel"
            android:textSize="@dimen/txt_16sp"
            app:font_style="regular"
            app:drawableEnd="@drawable/ic_remove_eye" />

    </android.support.design.widget.TextInputLayout>

上面的代码在XML中没有定义inputType

EditText password = (EditText) findViewById(R.id.password);
password.setTransformationMethod(new PasswordTransformationMethod());

在Java中,setTransformationMethod将帮助我获得textPassword输入类型的属性,我也很高兴我的自定义字体风格。

但是下面提到的崩溃发生在27.1.1支持库的所有API级别上。

null pointerexception:尝试调用虚方法void android.support.design.widget.CheckableImageButton.setChecked(布尔) 在空对象引用上

这是由于TextInputLayout类内的onRestoreInstanceState崩溃。

复制步骤:切换密码可见性和最小化应用程序,并从最近的应用程序打开。呃,何鸿燊坠毁!

我所需要的是默认密码切换选项(使用支持库)和自定义字体在密码输入字段。

过了一段时间后,通过如下方法计算出,

<android.support.design.widget.TextInputLayout
        android:id="@+id/input_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/_10dp"
        android:layout_marginTop="@dimen/_32dp"
        android:hint="@string/current_password"
        android:textColorHint="@color/hint_text_color"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:passwordToggleEnabled="true"
        app:passwordToggleTint="@color/black">


        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="start|left"
            android:maxLines="1"
            android:textAlignment="viewStart"
            android:textColor="@color/black"
            android:textColorHint="@color/camel"
            android:textSize="@dimen/txt_16sp"
            app:font_style="regular"
            app:drawableEnd="@drawable/ic_remove_eye"
            android:inputType="textPassword" />

    </android.support.design.widget.TextInputLayout>

在XML中,添加android:inputType="textPassword"

TextInputLayout inputPassword = findViewById(R.id.input_password);
EditText password = findViewById(R.id.password);
EditText userName = findViewById(R.id.user_name);
// Get the typeface of user name or other edit text
Typeface typeface = userName.getTypeface();
if (typeface != null)
   inputLayout.setTypeface(typeface); // set to password text input layout

在以上java代码中,

我从用户名EditText获得了自定义字体,并将其应用于密码字段的TextInputLayout。现在您不需要显式地将字体设置为密码EditText,因为它将获得TextInputLayout属性。

此外,我删除了密码。setTransformationMethod(新PasswordTransformationMethod ());

通过这样做,passwordToggleEnabled正在工作,自定义字体也被应用,拜拜崩溃。希望这个问题将在即将发布的支持版本中得到解决。

你也可以使用

<android.support.design.widget.TextInputLayout/>

在一起

<android.support.v7.widget.AppCompatEditText/>