当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" />

当前回答

像上面一样,但要确保字段在XML中没有粗体样式,因为即使有上面的修复,它们看起来也不会相同!

其他回答

您还可以使用自定义小部件。它非常简单,并且不会使你的Activity/Fragment代码变得混乱。

代码如下:

public class PasswordEditText extends EditText {

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

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

  }

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

  private void init() {
    setTypeface(Typeface.DEFAULT);
  }
}

你的XML看起来是这样的:

<com.sample.PasswordEditText
  android:id="@+id/password_edit_field"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:hint="Password"
  android:inputType="textPassword"
  android:password="true" />

像上面一样,但要确保字段在XML中没有粗体样式,因为即使有上面的修复,它们看起来也不会相同!

如果您将书法库与TextInputLayout和EditText结合使用,下面的代码可以很好地工作。

    EditText password = (EditText) findViewById(R.id.password);
    TextInputLayout passwordLayout = (TextInputLayout) findViewById(R.id.passwordLayout);

    Typeface typeface_temp = password.getTypeface();
    password.setInputType(InputType.TYPE_CLASS_TEXT |
            InputType.TYPE_TEXT_VARIATION_PASSWORD); 

    password.setTypeface(typeface_temp);
    passwordLayout.setTypeface(typeface_temp);

改变xml中的字体对我的提示文本也不起作用。我找到了两种不同的解决方案,其中第二种对我来说表现更好:

将android:inputType="textPassword"从你的xml文件中删除,改为在java中设置它: EditText password = (EditText) findViewById(R.id.password_text); 密码。setTransformationMethod(新PasswordTransformationMethod ());

使用这种方法,提示字体看起来不错,但当您在编辑字段中输入时,在它变成密码点之前,您不会看到纯文本中的每个字符。此外,全屏输入时,不会出现圆点,而是以明文显示密码。

在xml中保留android:inputType="textPassword"。在Java中,设置字体和密码方法: EditText password = (EditText) findViewById(R.id.register_password_text); password.setTypeface (Typeface.DEFAULT); 密码。setTransformationMethod(新PasswordTransformationMethod ());

这种方法给了我想要的提示字体,并给了我想要的使用密码点的行为。

你也可以使用

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

在一起

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