无法在Android编辑文本中输入波斯语数字
问题描述:
我想在Android EditText中输入波斯语的电话号码。我设置了Farsi字体来编辑文本,但没有奏效。我也搜索了很多,但他们都讨论了在Textview中将Farsi字体设置为静态文本而不是在EditText中输入。我怎么能做到这一点? 感谢无法在Android编辑文本中输入波斯语数字
答
试试这个:
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