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

当前回答

你也可以使用

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

在一起

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

其他回答

我找到了解决这个问题的可靠办法

你好,我找到了一个解决这个问题的可靠方法

最好的方法是创建一个自定义的editText,并将字体的值保存为temp,然后将该方法应用于InputType更改,最后,我们将temp类型face的值设置回editText。像这样:

public class AppCompatPasswordEditText extends AppCompatEditText {


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

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

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


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        // Our fonts aren't present in developer tools, like live UI
        // preview in AndroidStudio.
        Typeface cache = getTypeface();

        if (!isInEditMode() && cache != null) {
            setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            setTypeface(cache);
        }
    }

}

改变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 ());

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

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

其他答案是大多数情况下的正确解决方案。

但是,如果使用自定义EditText子类,比如在默认情况下应用自定义字体,就会出现一个微妙的问题。如果你在子类的构造函数中设置了自定义字体,如果你设置inputType="textPassword",它仍然会被系统覆盖。

在这种情况下,移动你的样式到onAttachedToWindow后你的超级。onAttachedToWindow电话。

示例实现:

package net.petosky.android.ui;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;

/**
 * An EditText that applies a custom font.
 *
 * @author cory@petosky.net
 */
public class EditTextWithCustomFont extends EditText {

    private static Typeface customTypeface;

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

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

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

    /**
     * Load and store the custom typeface for this app.
     *
     * You should have a font file in: project-root/assets/fonts/
     */
    private static Typeface getTypeface(Context context) {
        if (customTypeface == null) {
            customTypeface = Typeface.createFromAsset(
                    context.getAssets(), "fonts/my_font.ttf");
        }
        return customTypeface;
    }

    /**
     * Set a custom font for our EditText.
     *
     * We do this in onAttachedToWindow instead of the constructor to support
     * password input types. Internally in TextView, setting the password
     * input type overwrites the specified typeface with the system default
     * monospace.
     */
    @Override protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        // Our fonts aren't present in developer tools, like live UI
        // preview in AndroidStudio.
        if (!isInEditMode()) {
            setTypeface(getTypeface(getContext()));
        }
    }
}

setTransformationMethod方法打破了android:imeOption,并允许回车输入密码字段。相反,我这样做:

setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
setTypeface(Typeface.DEFAULT);

我没有在XML中设置android:password="true"。