如何将英语 "abc123 " 数字动态转换为阿拉伯语“ابث١٢٣” android

How to convert english "abc123 " digits to arabic "ابث١٢٣ " dynamically android

已更新

我一直在寻找解决方案,但还没有找到..

我试过这个

// Check for language
    String tempLang = Locale.getDefault().getLanguage();
    if (tempLang == "ar") {
        Locale AR_LOCALE_EAST_NUMBERS = new Locale.Builder().setLanguageTag("ar-u-nu-arab").build();
        Locale.setDefault(AR_LOCALE_EAST_NUMBERS);

        firstName.setKeyListener(DigitsKeyListener.getInstance(getActivity().getString(R.string.input_charatars_only_valid)));
    } else {

        firstName.setKeyListener(DigitsKeyListener.getInstance(getActivity().getString(R.string.input_charatars_only_valid)));
    }
  1. 首先它给出了 API 的错误 .. 它说使用

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)

  2. 我想用它 15 API 因为我的 minSdk 是 15

是否正确或是否需要任何更改

已更新

请告诉我如何将英文数字转换为

"abcdefghijklmnopqrstuvwxyz123456789 " 和一个空间也“”

如何使用 abc,123 & a space 将字符串转换为阿拉伯语?

PS:我是新来的,请忽略是否有任何错误.. 谢谢

你可以用 replaceAll 来完成。

首先,创建一个方法:

    public String convertToArabic(int value)
{
    String newValue = (((((((((((value+"")
            .replaceAll("1", "١")).replaceAll("2", "٢"))
            .replaceAll("3", "٣")).replaceAll("4", "٤"))
            .replaceAll("5", "٥")).replaceAll("6", "٦"))
            .replaceAll("7", "٧")).replaceAll("8", "٨"))
            .replaceAll("9", "٩")).replaceAll("0", "٠"));
    return newValue;
}

和用法:

String myArabicNumber= convertToArabic(123);
Log.d("output",myArabicNumber);

输出:

١٢٣ 

更新:

你可以通过连接它们来做到这一点:

    String space = "\u00A0"; //space
    String myArabicCharacter = getResources().getString(R.string.help);
    String myArabicNumber= convertToArabic(123);
    String fullHomework = myArabicCharacter+space+myArabicNumber;
    Log.d("output",fullHomework);

输出:

     راهنما ١٢٣

更新2

YourProject/
    res/
       values/
           strings.xml
       values-ar/
           strings.xml

arabic string resource:
    <string name="help">راهنما</string>

english string resource:
    <string name="help">help</string>

使用ascii字符转换

将数字 EN 转换为 AR

public static String convertNumberEnglishToArabic(String input) {

        String value = "";

        for (char character : input.toCharArray()) {

            String str = "";
            int ascii = (int) character;

            if (ascii >= 49 && ascii <= 51) {
                //english number
                int valueOld = ascii + 1584;
                char valueChar = (char) valueOld;
                str = String.valueOf(valueChar);
            } else {
                //default
                str = String.valueOf(character);
            }

            value += str;
        }
        return value;
}

用于将数字从 AR 转换为 EN

public String converNumberArabicToEnglish(String input) {

        String value = "";

        for (char character : input.toCharArray()) {

            String str = "";
            int ascii = (int) character;
            if (ascii >= 1632 && ascii <= 1641) {
                //arabic number
                int valueOld = ascii - 1584;
                char valueChar = (char) valueOld;
                str = String.valueOf(valueChar);
            } else {
                //default
                str = String.valueOf(character);
            }

            value += str;
        }
        return value;
}