简体   繁体   中英

Applying a custom typeface to an editText

I have 2 EditText(s) in my activity. One for a mobile no, and the other for a pin no. The mobile no editText has an inputType of phone:

Mobile No EditText

<android.support.design.widget.TextInputLayout
        android:id="@+id/tiMobileNoFirst"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="10dp">

        <myPackageName.CustomEditText
            android:id="@+id/etMobileNo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/mobile_no"
            android:inputType="phone"
            android:maxLength="12"
            android:maxLines="1"
            android:textColor="@color/black" />
    </android.support.design.widget.TextInputLayout>

and the pin no has an inputType of numberPassword:

PIN No EditText

<android.support.design.widget.TextInputLayout
        android:id="@+id/tiPINFirst"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp">

        <myPackageName.CustomEditText
            android:id="@+id/etPINFirst"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/pin"
            android:inputType="numberPassword"
            android:maxLength="4"
            android:maxLines="1"
            android:textColor="@color/black" />
    </android.support.design.widget.TextInputLayout>

my CustomEditText class looks like below:

public class CustomEditText extends AppCompatEditText {
public CustomEditText(Context context) {
    super(context);
    EditHelper.setTypeFace(context,this);
}

public CustomEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    EditHelper.setTypeFace(context,this);
}

public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    EditHelper.setTypeFace(context,this);
}
}

class EditHelper{
static Typeface typeface;

public static void setTypeFace(Context context, EditText editText){
    if(typeface == null){
        typeface = Typeface.createFromAsset(context.getAssets(),"fonts/canaro_light.otf");
    }
    editText.setTypeface(typeface,Typeface.BOLD);
}
}

Applying the custom Typeface to the mobile no works well, however on applying it to the pin no, doesn't work, it reverts back to the default android typeface.

Changing the inputType of the pin to something else works, however I need to use the numberPassword in this situation.

How can I accomplish the desired result?

numberPassword uses its own typeface.

what you can do is set input type of your pin no edittext as number and programmatically set its **transformation method* to password

etPINFirst.setTransformationMethod(new PasswordTransformationMethod());

and change android:inputType="numberPassword" to android:inputType="number" in your etPINFirst

在Java中设置inputType可以达到目的!

etPINFirst.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_VARIATION_PASSWORD);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM