如何在视图中显示波斯语(Farsi)数字

34

我想在视图中显示波斯(Farsi)数字。例如,我计算了一个日期并将其转换为波斯日历,但如何使用波斯数字显示它?


您可能需要使用一些dll或第三方工具来获取波斯语字体。 - Itban Saeed
13个回答

1

使用Kotlin编写无符号整数即可

fun toPersian(n:Int) : String{
val p="۰۱۲۳۴۵۶۷۸۹"
return n.toString().trim().map{
    p[it.toString().trim().toInt()]
}.joinToString()
}
txt_view?.text=toPersian(12087)//۱۲۰۸۷

1
为了管理应用程序的语言(英语/波斯语),应用程序中使用的字体必须正确转换和显示波斯语和英语数字。这就是为什么我们使用setTypeface()方法的原因:
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);
    }

使用 iranSansRegular 来设置字体:
public class MyTextView extends TextView {
        public MyTextView (Context context, AttributeSet attrs) {
            super(context, attrs);
            Typeface typeface = Typeface.createFromAsset(context.getAssets(), iranSansRegular);
            setTypeface(typeface);
        }
    }

-1

你必须在Windows中添加波斯标准键盘,并在想要输入波斯数字和单词时切换到该键盘。这对我很有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接