在Android中创建自定义EditText

时间:2014-10-02 14:15:06

标签: android fonts android-edittext typeface

我希望在整个项目中为我的所有EditText应用具有不同样式的新字体。我尝试通过创建CustomEditText类来扩展EditText,如下所示:

public class CustomEditText extends EditText {

public static String LATO_BOLD = "fonts/Lato-Bold.ttf";
public static String LATO_LIGHT = "fonts/Lato-Light.ttf";
public static String LATO_REGULAR = "fonts/Lato-Regular.ttf";
public static String LATO_BLACK = "fonts/Lato-Black.ttf";

private static final int BOLD = 1;
private static final int LIGHT = 2;
private static final int REGULAR = 3;
private static final int BLACK = 4;

public CustomEditText(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    init();
}

public CustomEditText(Context context) {
    this(context, null);
}

public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if (isInEditMode())
        return;

    TypedArray a = getContext().obtainStyledAttributes(attrs,
            R.styleable.com_pts_mm_android_views_CustomEditText, 0, 0);
    final int fontType = a.getInt(0, -1);

    a.recycle();
    switch (fontType) {
        case LIGHT:
             setLight();
             break;
        case BOLD:
             setBold();
             break;
        case REGULAR:
            init();
            break;
        case BLACK:
            setBlack();
            break;
        default:
            init();
            break;
    }

}

public void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
            LATO_REGULAR);
    this.setTypeface(tf);
}

public void setLight() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
            LATO_LIGHT);
    this.setTypeface(tf);
}

public void setBold() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
            LATO_BOLD);
    this.setTypeface(tf);
}

public void setBlack() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
            LATO_BLACK);
    this.setTypeface(tf);
}

}

我在/ assets / fonts文件夹下有我所有的字体文件(ttf文件)。然后在attrs.xml中定义属性和样式,如下所示:

<attr name="custom_font">
    <enum name="bold" value="1" />
    <enum name="light" value="2" />
    <enum name="regular" value="3" />
    <enum name="black" value="4" />    
</attr>

<declare-styleable name="com.pts.mm.android.views.CustomEditText">
    <attr name="custom_font" />
</declare-styleable>

所以现在我在我的布局中使用这个自定义EditText,如下所示:

    <com.pts.mm.android.views.CustomEditText
    android:id="@+id/etNric"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/edit_box_horizontal_padding"
    android:paddingRight="@dimen/edit_box_horizontal_padding"
    android:layout_marginBottom="@dimen/paragraph_vertical_margin"
    android:textSize="@dimen/text_edit_size"
    android:textColor="@color/text_edit_color"
    android:inputType="text"
    android:background="@drawable/textbox_userid1"
    custom_font="regular"/>

然而,这不起作用。我的EditText字段变得不可编辑(光标刚刚消失)。有没有人知道出了什么问题?感谢。

1 个答案:

答案 0 :(得分:0)

试试这个

editText.setTypeface(Typeface.SERIF);