密码提示字体与其他edittext字段不同

时间:2017-08-17 13:55:36

标签: android android-edittext android-typeface

EditText password = (EditText) view.findViewById(R.id.etPassword);

    if (Build.VERSION.SDK_INT != android.os.Build.VERSION_CODES.KITKAT) {
        Typeface typeface = TypefaceUtil.overrideFont(getContext(), "SERIF", "fonts/calibri.ttf");
        password.setTypeface(typeface);
    }

这是TextInputLayout

的XML布局
<android.support.design.widget.TextInputLayout
    android:id="@+id/inpPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/button_margin_small">

    <android.support.design.widget.EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:hint="@string/set_pass"
        android:inputType="textPassword"
        android:maxLength="@integer/pass_max"
        android:maxLines="1"
        android:password="true"
        android:singleLine="true" />
</android.support.design.widget.TextInputLayout>

我正在尝试使用此代码,但我的密码提示字体样式与我设置的不同。

6 个答案:

答案 0 :(得分:8)

添加到此,当iandroid:inputType =“textPassword”被删除并添加app:passwordToggleEnabled =“true”时,它会起作用

<android.support.design.widget.TextInputLayout
            android:id="@+id/inpPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/button_margin_small"
            app:passwordToggleEnabled="true">

            <EditText
                android:id="@+id/etPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/set_pass"
                android:maxLength="@integer/pass_max"
                android:maxLines="1"
                android:singleLine="true" />
        </android.support.design.widget.TextInputLayout>



EditText password = (EditText) view.findViewById(R.id.etPassword);
    password.setTypeface(Typeface.DEFAULT);
    password.setTransformationMethod(new PasswordTransformationMethod());

答案 1 :(得分:3)

使用android:fontFamily="sans-serif"

答案 2 :(得分:3)

尝试一下

<Edittext
            android:id="@+id/edtPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password"
            android:inputType="text"
            android:textSize="14sp" />

在您的edittext xml中设置android:inputType="text"

在Java文件中设置edtPassword.setTransformationMethod(new PasswordTransformationMethod());

以自定义字体为edittext为我工作。

答案 3 :(得分:2)

嘿,如果有人在使用kotlin,我希望这段代码对您有帮助,对我有用。我在用着 com.google.android.material.textfield.TextInputLayoutandroidx.appcompat.widget.AppCompatEditText

在科特林:

// find the view
val etPassword = view.findViewById<AppCompatEditText>(R.id.etPassword)

// set the typeface to your choice
etPassword.typeface = Typeface.SANS_SERIF

// the set its transformationMethod to PasswordTransformationMethod
etPassword.transformationMethod = PasswordTransformationMethod()

这是我的xml:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/tilPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:passwordToggleEnabled="true">


    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

答案 4 :(得分:1)

我也面临这个问题。

你必须尝试这种方式,你才能得到解决方案。

XML代码

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Password"
    android:id="@+id/password_text"/>

Java代码

    EditText password = (EditText) findViewById(R.id.password_text);
    password.setTypeface(Typeface.DEFAULT);
    password.setTransformationMethod(new PasswordTransformationMethod());

希望这会对你有帮助......

答案 5 :(得分:0)

我发现当输入类型为textPassword时,我的Edittext的字体样式不同。通过使用fontFamily属性修复此问题。

以下是我在布局中使用的代码:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif"
    android:hint="My Password hint"
    android:inputType="textPassword" />
相关问题