我想在视图中显示波斯(Farsi)数字。例如,我计算了一个日期并将其转换为波斯日历,但如何使用波斯数字显示它?
使用Kotlin编写无符号整数即可
fun toPersian(n:Int) : String{
val p="۰۱۲۳۴۵۶۷۸۹"
return n.toString().trim().map{
p[it.toString().trim().toInt()]
}.joinToString()
}
txt_view?.text=toPersian(12087)//۱۲۰۸۷
public class MyTextView extends TextView {
public MyTextView (Context context, AttributeSet attrs) {
super(context, attrs);
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/iransans_fa_number_regular.ttf");
setTypeface(typeface);
}
}
当应用程序的语言改变时,我们会更改MyTextView
中使用的字体:
public static String iranSansRegular;
public static void setLocale(Resources res, Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences("Configuration", 0);
String appLang = sharedPreferences.getString("appLanguage", Locale.getDefault().getLanguage());
if (appLang.equals("fa")) {
iranSansRegular = "fonts/iransans_fa_number_regular.ttf";
} else {
iranSansRegular = "fonts/iransans_en_number_regular.ttf";
}
Locale myLocale = new Locale(appLang);
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
Locale.setDefault(myLocale);
conf.setLayoutDirection(myLocale);
res.updateConfiguration(conf, dm);
}
public class MyTextView extends TextView {
public MyTextView (Context context, AttributeSet attrs) {
super(context, attrs);
Typeface typeface = Typeface.createFromAsset(context.getAssets(), iranSansRegular);
setTypeface(typeface);
}
}
你必须在Windows中添加波斯标准键盘,并在想要输入波斯数字和单词时切换到该键盘。这对我很有效。
dll
或第三方工具来获取波斯语字体。 - Itban Saeed