2017-09-14 56 views
-2

我想在Android EditText中輸入波斯語的電話號碼。我設置了Farsi字體來編輯文本,但沒有奏效。我也搜索了很多,但他們都討論了在Textview中將Farsi字體設置爲靜態文本而不是在EditText中輸入。我怎麼能做到這一點? 感謝無法在Android編輯文本中輸入波斯語數字

回答

2

試試這個:

public class CustomFontEditText extends EditText { 


private Context context; 
private AttributeSet attrs; 
private int defStyle; 

public CustomFontEditText(Context context) { 
    super(context); 
    this.context=context; 
    init(); 
} 

public CustomFontEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.context=context; 
     this.attrs=attrs; 
     init(); 
} 

public CustomFontEditText(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     this.context=context; 
     this.attrs=attrs; 
     this.defStyle=defStyle; 
     init(); 
} 

private void init() { 
     Typeface font=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf"); 
     this.setTypeface(font); 
} 
@Override 
public void setTypeface(Typeface tf, int style) { 
    tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/farsi.ttf"); 
    super.setTypeface(tf, style); 
} 

@Override 
public void setTypeface(Typeface tf) { 
    tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/farsi.ttf"); 
    super.setTypeface(tf); 
} 

注:

替換farsi.ttf您的字體名稱和在XML使用CustomFontEditText而不是默認EditText

相關問題