如何将英文“abc123”数字转换为阿拉伯语“ابث123”动态android

时间:2017-07-12 09:18:17

标签: java android arabic digits

已更新

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

我试过这个

// 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

  3. 是正确还是需要进行任何更改

    更新

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

    “abcdefghijklmnopqrstuvwxyz123456789”和sapce也“”

    如何将字符串转换为阿拉伯语与abc,123&空间?

    PS:我是新来的,请不要理睬任何错误。 感谢

2 个答案:

答案 0 :(得分:2)

您可以使用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);

输出:

     راهنما ١٢٣

<强> UPDATE2

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

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

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

答案 1 :(得分:2)

使用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;
}