2014-10-02 92 views
0

我想为我的项目中的所有EditText应用不同样式的新字体。我试图通过创建作为下方延伸的EditText CustomEditText类来做到这一点:在Android中创建自定义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); 
} 

}

我有我所有的字体文件(TTF文件)下/资产/ fonts文件夹中。那么属性和stylables在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字段变为不可编辑(光标消失)。有没有人有任何线索出了什么问题?谢谢。

+0

您的editText xml上的'custom_font'的前缀在哪里? – 2014-10-02 14:24:26

+0

好的,我试图按照这个指令定义命名空间[link](http://developer.android.com/training/custom-views/create-view.html),但它不起作用。 – 2014-10-04 09:25:54

回答

0

试试这个

editText.setTypeface(Typeface.SERIF);

+0

在代码中设置字体是好的,但我想用xml中的自定义属性来实现它。 – 2014-10-04 09:16:18