如何支持阿拉伯文字的Andr​​oid? [英] How to support Arabic text in Android?

查看:137
本文介绍了如何支持阿拉伯文字的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到阿拉伯文字从服务器成功。检索到的文本我要显示在code,但其显示框而不是阿拉伯文字。假设 T 数组的值是从服务器的阿拉伯文字。

 的String [] T = {};
TextView的电视=(TextView中)findviewByid(R.id.text);
tv.setText(吨[0]);
 

解决方案

Android 2.1的没有阿拉伯字体。

Android 2.2的有阿拉伯字体,但没有正确显示你的话。

安卓3.x的完全支持阿拉伯语。

有关Android 2.1系统,你必须设置字体 Farsi.GetFarsiFont(本)然后用 Farsi.Convert(سلام)

有关Android 2.2的你并不需要设置字体,但必须使用 Farsi.Convert(سلام)

和为Android 3.X忘记所有的上述解决方案)。但你可以改变字体,如果你不喜欢3.x的阿拉伯字体。

所以做以下步骤:

1)首先,在资产的文件夹中添加TAHOMA.TTF

2)使用下面的类来获取字体和您的阿拉伯语或波斯语的文字转换成一个很好的形式显示在你的活动。

 进口android.content.Context;
进口android.graphics.Typeface;

公共final类波斯语{

    公共静态布尔isFarsiConversionNeeded = TRUE;

    私人最终静态字符串szLamAndAlef =字符
            的ToString((炭)0xfedf)
            + Character.toString((char)的0xfe8e); //林+阿勒夫

    私人最终静态字符串szLamStickAndAlef =字符
            的ToString((炭)0xfee0)
            + Character.toString((char)的0xfe8e); //林(置顶!)+
                                                    //阿勒夫

    私人最终静态字符串szLa = Character.toString((char)的0xfefb); //香格里拉
    私人最终静态字符串szLaStick = Character.toString((char)的0xfefc); //香格里拉
                                                                                // (黏!!!)

    私人最终静态字符串szLamAndAlefWoosim =字符
            的ToString((炭)0xe1)
            + Character.toString((炭)为0xBB); //林+阿勒夫

    私人最终静态字符串szLamStickAndAlefWoosim =字符
            的ToString((炭)0×90)
            + Character.toString((炭)为0xBB); //林(置顶!)+
                                                //阿勒夫

    私人最终静态字符串szLaWoosim = Character.toString((char)的0xd9); //香格里拉
    私人最终静态字符串szLaStickWoosim =字符
            的ToString((char)的0xd9); //香格里拉

    // (黏!!!)

    私有静态final类STRUC {
        公共CHAR字符;
        公共字符endGlyph;
        公共字符iniGlyph;
        公共字符midGlyph;
        公共字符isoGlyph;

        公共STRUC(CHAR字符,字符EndGlyph,焦炭IniGlyph,
                炭MidGlyph,焦炭IsoGlyph){
            字符=字符;
            endGlyph = EndGlyph;
            iniGlyph = IniGlyph;
            midGlyph = MidGlyph;
            isoGlyph = IsoGlyph;
        }
    }

    静态STRUC [] arrStruc = {
            新STRUC((炭)0x630,(炭)0xfeac,(炭)0xfeab,
                    (炭)0xfeac,(炭)0xfeab)
            新STRUC((炭)0x62f,(炭)0xfeaa,(炭)0xfea9,
                    (炭)0xfeaa,(炭)0xfea9)
            新STRUC((炭)0x62c,(炭)0xfe9e,(炭)0xfe9f,
                    (炭)0xfea0,(炭)0xfe9d)
            新STRUC((炭)0x62d,(炭)0xfea2,(炭)0xfea3,
                    (炭)0xfea4,(炭)0xfea1)
            新STRUC((炭)0x62e,(炭)0xfea6,(炭)0xfea7,
                    (炭)0xfea8,(炭)0xfea5)
            新STRUC((炭)0x647,(炭)0xfeea,(炭)0xfeeb,
                    (炭)0xfeec,(炭)0xfee9)
            新STRUC((炭)0x639,(炭)0xfeca,(炭)0xfecb,
                    (炭)0xfecc,(炭)0xfec9)
            新STRUC((炭)0x63a,(炭)0xfece,(炭)0xfecf,
                    (炭)0xfed0,(炭)0xfecd)
            新STRUC((炭)0x641,(炭)0xfed2,(炭)0xfed3,
                    (炭)0xfed4,(炭)0xfed1)
            新STRUC((炭)0x642,(炭)0xfed6,(炭)0xfed7,
                    (炭)0xfed8,(炭)0xfed5)
            新STRUC((炭)0x62b,(炭)0xfe9a,(炭)0xfe9b,
                    (炭)0xfe9c,(炭)0xfe99)
            新STRUC((炭)0x635,(炭)0xfeba,(炭)0xfebb,
                    (炭)0xfebc,(炭)0xfeb9)
            新STRUC((炭)0x636,(炭)0xfebe,(炭)0xfebf,
                    (炭)0xfec0,(炭)0xfebd)
            新STRUC((炭)0x637,(炭)0xfec2,(炭)0xfec3,
                    (炭)0xfec4,(炭)0xfec1)
            新STRUC((炭)0x643,(炭)0xfeda,(炭)0xfedb,
                    (炭)0xfedc,(炭)0xfed9)
            新STRUC((炭)0x645,(炭)0xfee2,(炭)0xfee3,
                    (炭)0xfee4,(炭)0xfee1)
            新STRUC((炭)0x646,(炭)0xfee6,(炭)0xfee7,
                    (炭)0xfee8,(炭)0xfee5)
            新STRUC((炭)0x62a,(炭)0xfe96,(炭)0xfe97,
                    (炭)0xfe98,(炭)0xfe95)
            新STRUC((炭)0x627,(炭)0xfe8e,(炭)0xfe8d,
                    (炭)0xfe8e,(炭)0xfe8d)
            新STRUC((炭)0x644,(炭)0xfede,(炭)0xfedf,
                    (炭)0xfee0,(炭)0xfedd)
            新STRUC((炭)0x628,(炭)0xfe90,(炭)0xfe91,
                    (炭)0xfe92,(炭)0xfe8f)
            新STRUC((炭)0x64a,(炭)0xfef2,(炭)0xfef3,
                    (炭)0xfef4,(炭)0xfef1)
            新STRUC((炭)0x633,(炭)0xfeb2,(炭)0xfeb3,
                    (炭)0xfeb4,(炭)0xfeb1)
            新STRUC((炭)0x634,(炭)0xfeb6,(炭)0xfeb7,
                    (炭)0xfeb8,(炭)0xfeb5)
            新STRUC((炭)0x638,(炭)0xfec6,(炭)0xfec7,
                    (炭)0xfec8,(炭)0xfec5)
            新STRUC((炭)0x632,(炭)0xfeb0,(炭)0xfeaf,
                    (炭)0xfeb0,(炭)0xfeaf)
            新STRUC((炭)0x648,(炭)0xfeee,(炭)0xfeed,
                    (炭)0xfeee,(炭)0xfeed)
            新STRUC((炭)0x629,(炭)0xfe94,(炭)0xfe93,
                    (炭)0xfe93,(炭)0xfe93)
            新STRUC((炭)0x649,(炭)0xfef0,(炭)0xfeef,
                    (炭)0xfef0,(炭)0xfeef)
            新STRUC((炭)0x631,(炭)0xfeae,(炭)0xfead,
                    (炭)0xfeae,(炭)0xfead)
            新STRUC((炭)0x624,(炭)0xfe86,(炭)0xfe85,
                    (炭)0xfe86,(炭)0xfe85)
            新STRUC((炭)0x621,(炭)0xfe80,(炭)0xfe80,
                    (炭)0xfe80,(炭)0xfe80)
            新STRUC((炭)0x626,(炭)0xfe8a,(炭)0xfe8b,
                    (炭)0xfe8c,(炭)0xfe89)
            新STRUC((炭)0x623,(炭)0xfe84,(炭)0xfe83,
                    (炭)0xfe84,(炭)0xfe83)
            新STRUC((炭)0x622,(炭)0xfe82,(炭)0xfe81,
                    (炭)0xfe82,(炭)0xfe81)
            新STRUC((炭)0x625,(炭)0xfe88,(炭)0xfe87,
                    (炭)0xfe88,(炭)0xfe87)
            新STRUC((炭)0x67e,(炭)0xfb57,(炭)0xfb58,
                    (炭)0xfb59,(炭)0xfb56),// PEH
            新STRUC((炭)0x686,(炭)0xfb7b,(炭)0xfb7c,
                    (炭)0xfb7d,(炭)0xfb7a),//张彻
            新STRUC((炭)0x698,(炭)0xfb8b,(炭)0xfb8a,
                    (炭)0xfb8b,(炭)0xfb8a),// JEH
            新STRUC((炭)0x6a9,(炭)0xfb8f,(炭)0xfb90,
                    (炭)0xfb91,(炭)0xfb8e),// keheh
            新STRUC((炭)0x6af,(炭)0xfb93,(炭)0xfb94,
                    (炭)0xfb95,(炭)0xfb92),// GAF
            //新STRUC((炭)0x6cc,(炭)0xfbfd,(炭)0xfbfe,
            //(炭)0xfbff,(炭)0xfbfc),//波斯语叶
            新STRUC((炭)0x6cc,(炭)0xfbfd,(炭)0xfef3,
                    (炭)0xfef4,(炭)0xfbfc),//阿拉伯语叶
            新STRUC((炭)0x6c0,(炭)0xfba5,(炭)0xfba4,
                    (炭)0xfba5,(炭)0xfba4)//港灯与叶
    };

    静态STRUC [] arrStrucWoosim = {
            新STRUC((炭)0x630,(炭)0xB5执行,(炭)为0x82,(炭)0xB5执行,
                    (炭)为0x82),
            新STRUC((炭)0x62f,(炭)0xb4,(炭)0×81,(炭)0xb4,
                    (焦)0×81),
            新STRUC((炭)0x62c,(字符)0x9b,(炭)0xb1,(炭)0xf9,
                    (炭)为0xBF)
            新STRUC((炭)0x62d,(炭)为0x9c,(炭)0xb2,(炭)0xFA回应,
                    (炭)为0xC0)
            新STRUC((炭)0x62e,(炭)0x9d,(炭)0xb3,(炭)0xFE的,
                    (炭)0xc1)
            新STRUC((炭)0x647,(炭)0xac,(炭)0xe4,(焦炭)0x93,
                    (炭)0xd5)
            新STRUC((炭)0x639,(炭)0xc9,(炭)0xd3,(字符)0x8b,
                    (炭)0xa4)
            新STRUC((炭)0x63a,(炭)0xca,(炭)0xdd,(炭)0x8c,
                    (焦炭)的0xA5)
            新STRUC((炭)0x641,(炭)0xa6,(炭)写0xDE,(炭)0x8d,
                    (焦炭)的0xCC)
            新STRUC((炭)0x642,(炭)0xa7,(炭)0xdf,(炭)为0x8E,
                    (炭)0xce)
            新STRUC((炭)0x62b,(炭)0xbd,(炭)0XAF,(炭)0xea,
                    (焦)0x99),
            新STRUC((炭)0x635,(炭)0xc4,(炭)0xc8,(炭)87H的,
                    (炭)0XA0)
            新STRUC((炭)0x636,(炭)0xc5,(炭)0xcb,(焦)均为0x88,
                    (炭)0xa1)
            新STRUC((炭)0x637,(炭)0xc6,(炭)0XCD,(炭)0XCD,
                    (炭)0xa2)
            新STRUC((炭)0x643,(炭)0xcf,(炭)0xe0的,(炭)0x8f,
                    (炭)0xa8)
            新STRUC((炭)0x645,(炭)0xd2,(炭)0xe2,(焦炭)0x91的信息,
                    (焦炭)和0xAA)
            新STRUC((炭)0x646,(炭)0xd4,(炭)0xe3,(焦炭)0x92,
                    (炭)是0xAB)
            新STRUC((炭)0x62a,(炭)0xbd,(炭)0XAF,(炭)0xea,
                    (焦)0x99),
            新STRUC((炭)0x627,(炭)为0xBB,(焦炭)0x80的,(炭)为0xBB,
                    (焦)0x80的),
            新STRUC((炭)0x644,(炭)0xd1,(炭)0xe1,(炭)0×90,
                    (炭)0xa9)
            新STRUC((炭)0x628,(炭)0xbc,(炭)0xae,(炭)0xe9,
                    (焦)0x98在全局),
            新STRUC((炭)0x64a,(焦炭)的0xDC,(炭)0xe6,(炭)位0x95,
                    (焦炭)的0xDC)
            新STRUC((炭)0x633,(炭)为0xC2,(焦炭)的0xB8,(炭)的0xB8,
                    (炭)0x9e)
            新STRUC((炭)0x634,(炭)0xc3,(炭)0xb9,(炭)0xb9,
                    (炭)0x9f),
            新STRUC((炭)0x638,(炭)0xc7,(炭)0XCD,(炭)0XCD,
                    (炭)0xc7)
            新STRUC((炭)0x632,(炭)0xb7,(炭)0xb7,(炭)0xb7,
                    (炭)0xb7)
            新STRUC((炭)0x648,(焦)0x94的,(焦炭)0x94的,(焦炭)0x94的,
                    (焦)0x94的),
            新STRUC((炭)0x629,(炭)0xda,(炭)0xda,(炭)0xda,
                    (炭)0xda)
            新STRUC((炭)0x649,(炭)的0xDC,(炭)0xe6,(炭)位0x95,
                    (焦炭)的0xDC)
            新STRUC((炭)0x631,(炭)0xb6,(炭)0xb6,(炭)0xb6,
                    (炭)0xb6)
            新STRUC((炭)0x624,(炭)0xe7,(炭)0xe7,(炭)0xe7,
                    (炭)0xe7)
            新STRUC((炭)0x621,(炭)0xba,(炭)0xba,(炭)0xba,
                    (炭)0xba)
            新STRUC((炭)0x626,(炭)0xd7,(炭)0xe8,(焦炭)0x97,
                    (炭)0xd7)
            新STRUC((炭)0x623,(焦)0x80的,(焦炭)0x80的,(焦炭)0x80的,
                    (焦)0x80的),
            新STRUC((炭)0x622,(焦)0x80的,(焦炭)0x80的,(焦炭)0x80的,
                    (焦)0x80的),
            新STRUC((炭)0x625,(焦)0x80的,(焦炭)0x80的,(焦炭)0x80的,
                    (焦)0x80的),
            新STRUC((炭)0x67e,(炭)0xbc,(炭)0xae,(炭)0xe9,
                    (焦)0x98在全局),// PEH
            新STRUC((炭)0x686,(字符)0x9b,(炭)0xb1,(炭)0xf9,
                    (炭)为0xBF),//张彻
            新STRUC((炭)0x698,(炭)0xb7,(炭)0xb7,(炭)0xb7,
                    (炭)0xb7),// JEH
            新STRUC((炭)0x6a9,(炭)0xcf,(炭)0xe0的,(炭)0x8f,
                    (炭)0xa8)// keheh
            新STRUC((炭)0x6af,(炭)0xcf,(炭)0xe0的,(炭)0x8f,
                    (炭)0xa8)// GAF
            新STRUC((炭)0x6cc,(焦炭)的0xDC,(炭)0xe6,(炭)位0x95,
                    (焦炭)的0xDC),//叶
            新STRUC((炭)0x6c0,(炭)0xac,(炭)0xe4,(焦炭)0x93,
                    (炭)0xd5)//港灯与叶
    };

    私有静态最终诠释N_DISTINCT_CHARACTERS = 43;

    私有静态最后弦乐ArabicReverse(String s)将{
        尝试 {
            串出=,转;
            S = MakeReverse(多个);
            的char [] CHS =新的char [s.length()];
            CHS = s.toCharArray();
            INT I = 0;
            而(ⅰ&其中; s.length()){
                如果((CHS [I]≥='0'和;&安培; CHS [1]  -  ='9'))// ISDIGIT(S [i]于)?
                {
                    REV =;
                    而(I< s.length()
                            &功放;&安培; ((CHS [I]≥='0'和;&安培; CHS [1]  -  ='9')|| CHS [I] =='/'))// ISDIGIT(S [i]于)
                                                                                    //?
                    {
                        REV = REV + CHS [I]
                        ++我;
                    }
                    REV = MakeReverse(REV);
                    输出=输出+转;
                } 其他 {
                    输出=输出+ CHS [I]
                    ++我;
                }
            }
            S =输出;
        }赶上(例外前){
            //抛出新的异常(
            //异常发生在ArabicReverse功能。\\ N
            // + ex.getMessage());
        }
        返回S;
    }

    私有静态最终布尔isFromTheSet1(/ * WCHAR * /炭CH){
        的char [] theSet1 =新的char [] {(炭)0x62c,(炭)0x62d,(炭)0x62e,
                (炭)0x647,(炭)0x639,(炭)0x63a,(炭)0x641,
                (炭)0x642,(炭)0x62b,(炭)0x635,(炭)0x636,
                (炭)0x637,(炭)0x643,(炭)0x645,(炭)0x646,
                (炭)0x62a,(炭)0x644,(炭)0x628,(炭)0x64a,
                (炭)0x633,(炭)0x634,(炭)0x638,(炭)0x67e,
                (炭)0x686,(炭)0x6a9,(炭)0x6af,(炭)0x6cc,
                (炭)0x626};
        INT I = 0;
        而(I< 28){
            如果(CH == theSet1 [I])
                返回true;
            ++我;
        }
        返回false;
    }

    私有静态最终布尔isFromTheSet2(/ * WCHAR * /炭CH){
        的char [] theSet2 =新的char [] {(炭)0x627,(炭)0x623,(炭)0x625,
                (炭)0x622,(炭)0x62f,(炭)0x630,(炭)0x631,
                (炭)0x632,(炭)0x648,(炭)0x624,(炭)0x629,
                (炭)0x649,(炭)0x698,(炭)0x6c0};
        INT I = 0;
        而(ⅰ&所述; 14){
            如果(CH == theSet2 [I])
                返回true;
            ++我;
        }
        返回false;
    }

    私有静态最后弦乐MakeReverse(字符串文本){
        字符串结果=;
        的char [] CTEXT =新的char [text.length()];
        CTEXT = text.toCharArray();
        的for(int i =(text.length()) -  1; I> = 0;我 - ){
            结果+ = CTEXT [I]
        }
        返回结果;
    }

    公共静态最后弦乐ConvertBackToRealFarsi(字符串){

        如果(!isFarsiConversionNeeded){
            返回;
        }

        字符串斯特劳特=;
        StringBuilder的strBuilder =新的StringBuilder();
        INT I = 0;
        INT J = 0;
        的char []下巴=新的char [In.length()];
        下巴= In.toCharArray();

        对于(i = 0; I< In.length();我++){
            布尔发现= FALSE;
            为(J = 0; J< arrStruc.length; J ++){
                如果(下巴[我] == arrStruc [J] .midGlyph ||
                        下巴[我] == arrStruc [J] .iniGlyph ||
                        下巴[我] == arrStruc [J] .endGlyph ||
                        下巴[我] == arrStruc [J] .isoGlyph){
                    strBuilder.append(arrStruc [J] .character);
                    发现= TRUE;
                    打破;
                }
            }
            如果(!找到)
                strBuilder.append(下巴[I]);
        }

        斯特劳特= strBuilder.toString();
    斯特劳特= strOut.replace(szLaلا);
    斯特劳特= strOut.replace(szLaStickلا);

        返回斯特劳特;
    }

    公共静态最终字符串转换(字符串){

        如果(!isFarsiConversionNeeded){
            返回;
        }

        如果(在== NULL){
            返回 ;
        }

        布尔linkBefore,linkAfter;
        串出= IN;
        的char [] CHOUT =新的char [Out.length()];
        CHOUT = Out.toCharArray();
        的char []下巴=新的char [In.length()];
        下巴= In.toCharArray();

        的for(int i = 0; I< In.length();我++){
            / * WCHAR * /
            焦炭CH =下巴[I]
            如果((CH> =(char)的0x0621和放大器;&安培; CH< =(焦炭)0x064a)
                    || (CH ==(char)的0x067e)|| (CH ==(炭)0x0686)
                    || (CH ==(char)的0x0698)|| (CH ==(炭)0x06a9)
                    || (CH ==(char)的0x06af)|| (CH ==(炭)0x06cc)
                    || (CH ==(char)的0x06c0)); //是一个波斯语的性格?
            {
                INT IDX = 0;
                而(IDX< N_DISTINCT_CHARACTERS){
                    如果(arrStruc [IDX] .character ==下巴[I])
                        打破;
                    + IDX;
                }

                如果(ⅰ== In.length() -  1)
                    linkAfter = FALSE;
                其他
                    linkAfter =(isFromTheSet1(下巴[I + 1])|| isFromTheSet2(下巴[I + 1]));
                如果(我== 0)
                    linkBefore = FALSE;
                其他
                    linkBefore = isFromTheSet1(下巴[我 -  1]);
                如果(IDX< N_DISTINCT_CHARACTERS){
                    如果(linkBefore&安培;&安培; linkAfter)
                        CHOUT [i] = arrStruc [IDX] .midGlyph;
                    如果(linkBefore&安培;&安培;!linkAfter)
                        CHOUT [i] = arrStruc [IDX] .endGlyph;
                    如果(linkBefore&安培;!&安培; linkAfter)
                        CHOUT [i] = arrStruc [IDX] .iniGlyph;
                    如果(linkBefore&安培;!&安培;!linkAfter)
                        CHOUT [i] = arrStruc [IDX] .isoGlyph;
                } 其他 {
                    CHOUT [i] =下巴[I]
                }
            } 其他 {
                CHOUT [i] =下巴[I]
            }
        }
        输出=;
        对于(INT J = 0; J< chOut.length; J ++)
            输出+ = CHOUT [J]。
        //时间= ArabicReverse(出);

        输出= Out.replace((炭)0x200c,''); //改变没有空间空间

        输出= Out.replace(szLamAndAlef,szLa); //加入'林'和'阿勒夫和
                                                //使香格里拉
        输出= Out.replace(szLamStickAndAlef,szLaStick); //加入蓝棒
                                                            //和'阿勒夫
                                                            //做出'香格里拉
                                                            // 棒'

        返回reorderWords(出);

    }

    私人最终静态字符串reorderWords(字符串〜应变)
    {

        最终诠释ST_RTL = 0;
        最终诠释ST_LTR = 1;

        字符串斯特劳特=;
        字符串prevWord =;
        诠释状态= ST_RTL;
        的char [] ARR = strIn.toCharArray();
        INT I = 0;
        而(I< arr.length){
            如果(charIsLTR(ARR [I])及和放大器;!状态= ST_LTR)
            {
                //状态改变为LTR
                状态= ST_LTR;
                斯特劳特= prevWord +斯特劳特;
                prevWord =;
                prevWord + =改编[I]
            }
            否则,如果(charIsRTL(ARR [I])及和放大器;!状态= ST_RTL)
            {
                //状态改变为RTL
                状态= ST_RTL;
                斯特劳特= prevWord +斯特劳特;
                prevWord =;
                prevWord + =改编[I]
            }
            其他
            {
                //状态没有改变
                prevWord + =改编[I]
            }
            我++;
        }

        斯特劳特= prevWord +斯特劳特;

        返回斯特劳特;

    }

    私人最终静态布尔charIsLTR(焦CH)
    {
        返程(CH> =(char)的65安培; CH< =(炭)122)
                |
                Character.isDigit(CH);
    }

    私人最终静态布尔charIsRTL(焦CH)
    {
        返回CH> =(焦炭)0x0621;
    }

    私有静态字样字样;

    公共静态最后的字样GetFarsiFont(上下文的背景下){
        如果(字体== NULL){
            字体= Typeface.createFromAsset(context.getAssets(),
                    //DroidSansFallback.ttf);
                    TAHOMA.TTF);
        }
        返回字样;
    }
}
 

3),并在你的活动:

 字样TF = Farsi.GetFarsiFont(本);
MyTextView.setTypeface(TF);
MyTextView.setText(Farsi.Convert(سلام));
 

享受!

我的波斯语类的引用: Arabizer

I am getting Arabic text from server successfully. Retrieved text I want display in code but its showing boxes instead of Arabic text. Assume that t array values are Arabic text from server.

string[] t={" "};
Textview tv = (Textview) findviewByid(R.id.text);
tv.setText(t[0]);

解决方案

Android 2.1 does not have Arabic font.

Android 2.2 has Arabic font but does not show your word correctly.

Android 3.x supports Arabic completely.

For Android 2.1 you must set the typeface Farsi.GetFarsiFont(this) and then use Farsi.Convert("سلام")

For Android 2.2 you do not need setting font but must use Farsi.Convert("سلام")

And for Android 3.x forget all of the above solutions ;). But you can change the font if you do not like the 3.x Arabic font.

so do the following steps:

1) At first add "TAHOMA.TTF" in assets folder

2) use the following class to get the font and converting your Arabic or Farsi text to a good form for showing in your activities.

import android.content.Context;
import android.graphics.Typeface;

public final class Farsi {

    public static boolean isFarsiConversionNeeded = true;

    private final static String szLamAndAlef = Character
            .toString((char) 0xfedf)
            + Character.toString((char) 0xfe8e); // Lam + Alef

    private final static String szLamStickAndAlef = Character
            .toString((char) 0xfee0)
            + Character.toString((char) 0xfe8e); // Lam (Sticky !!!)+
                                                    // Alef

    private final static String szLa = Character.toString((char) 0xfefb); // La
    private final static String szLaStick = Character.toString((char) 0xfefc); // La
                                                                                // (Sticky!!!)

    private final static String szLamAndAlefWoosim = Character
            .toString((char) 0xe1)
            + Character.toString((char) 0xbb); // Lam + Alef

    private final static String szLamStickAndAlefWoosim = Character
            .toString((char) 0x90)
            + Character.toString((char) 0xbb); // Lam (Sticky !!!)+
                                                // Alef

    private final static String szLaWoosim = Character.toString((char) 0xd9); // La
    private final static String szLaStickWoosim = Character
            .toString((char) 0xd9); // La

    // (Sticky!!!)

    private static final class struc {
        public char character;
        public char endGlyph;
        public char iniGlyph;
        public char midGlyph;
        public char isoGlyph;

        public struc(char Character, char EndGlyph, char IniGlyph,
                char MidGlyph, char IsoGlyph) {
            character = Character;
            endGlyph = EndGlyph;
            iniGlyph = IniGlyph;
            midGlyph = MidGlyph;
            isoGlyph = IsoGlyph;
        }
    }

    static struc[] arrStruc = {
            new struc((char) 0x630, (char) 0xfeac, (char) 0xfeab,
                    (char) 0xfeac, (char) 0xfeab),
            new struc((char) 0x62f, (char) 0xfeaa, (char) 0xfea9,
                    (char) 0xfeaa, (char) 0xfea9),
            new struc((char) 0x62c, (char) 0xfe9e, (char) 0xfe9f,
                    (char) 0xfea0, (char) 0xfe9d),
            new struc((char) 0x62d, (char) 0xfea2, (char) 0xfea3,
                    (char) 0xfea4, (char) 0xfea1),
            new struc((char) 0x62e, (char) 0xfea6, (char) 0xfea7,
                    (char) 0xfea8, (char) 0xfea5),
            new struc((char) 0x647, (char) 0xfeea, (char) 0xfeeb,
                    (char) 0xfeec, (char) 0xfee9),
            new struc((char) 0x639, (char) 0xfeca, (char) 0xfecb,
                    (char) 0xfecc, (char) 0xfec9),
            new struc((char) 0x63a, (char) 0xfece, (char) 0xfecf,
                    (char) 0xfed0, (char) 0xfecd),
            new struc((char) 0x641, (char) 0xfed2, (char) 0xfed3,
                    (char) 0xfed4, (char) 0xfed1),
            new struc((char) 0x642, (char) 0xfed6, (char) 0xfed7,
                    (char) 0xfed8, (char) 0xfed5),
            new struc((char) 0x62b, (char) 0xfe9a, (char) 0xfe9b,
                    (char) 0xfe9c, (char) 0xfe99),
            new struc((char) 0x635, (char) 0xfeba, (char) 0xfebb,
                    (char) 0xfebc, (char) 0xfeb9),
            new struc((char) 0x636, (char) 0xfebe, (char) 0xfebf,
                    (char) 0xfec0, (char) 0xfebd),
            new struc((char) 0x637, (char) 0xfec2, (char) 0xfec3,
                    (char) 0xfec4, (char) 0xfec1),
            new struc((char) 0x643, (char) 0xfeda, (char) 0xfedb,
                    (char) 0xfedc, (char) 0xfed9),
            new struc((char) 0x645, (char) 0xfee2, (char) 0xfee3,
                    (char) 0xfee4, (char) 0xfee1),
            new struc((char) 0x646, (char) 0xfee6, (char) 0xfee7,
                    (char) 0xfee8, (char) 0xfee5),
            new struc((char) 0x62a, (char) 0xfe96, (char) 0xfe97,
                    (char) 0xfe98, (char) 0xfe95),
            new struc((char) 0x627, (char) 0xfe8e, (char) 0xfe8d,
                    (char) 0xfe8e, (char) 0xfe8d),
            new struc((char) 0x644, (char) 0xfede, (char) 0xfedf,
                    (char) 0xfee0, (char) 0xfedd),
            new struc((char) 0x628, (char) 0xfe90, (char) 0xfe91,
                    (char) 0xfe92, (char) 0xfe8f),
            new struc((char) 0x64a, (char) 0xfef2, (char) 0xfef3,
                    (char) 0xfef4, (char) 0xfef1),
            new struc((char) 0x633, (char) 0xfeb2, (char) 0xfeb3,
                    (char) 0xfeb4, (char) 0xfeb1),
            new struc((char) 0x634, (char) 0xfeb6, (char) 0xfeb7,
                    (char) 0xfeb8, (char) 0xfeb5),
            new struc((char) 0x638, (char) 0xfec6, (char) 0xfec7,
                    (char) 0xfec8, (char) 0xfec5),
            new struc((char) 0x632, (char) 0xfeb0, (char) 0xfeaf,
                    (char) 0xfeb0, (char) 0xfeaf),
            new struc((char) 0x648, (char) 0xfeee, (char) 0xfeed,
                    (char) 0xfeee, (char) 0xfeed),
            new struc((char) 0x629, (char) 0xfe94, (char) 0xfe93,
                    (char) 0xfe93, (char) 0xfe93),
            new struc((char) 0x649, (char) 0xfef0, (char) 0xfeef,
                    (char) 0xfef0, (char) 0xfeef),
            new struc((char) 0x631, (char) 0xfeae, (char) 0xfead,
                    (char) 0xfeae, (char) 0xfead),
            new struc((char) 0x624, (char) 0xfe86, (char) 0xfe85,
                    (char) 0xfe86, (char) 0xfe85),
            new struc((char) 0x621, (char) 0xfe80, (char) 0xfe80,
                    (char) 0xfe80, (char) 0xfe80),
            new struc((char) 0x626, (char) 0xfe8a, (char) 0xfe8b,
                    (char) 0xfe8c, (char) 0xfe89),
            new struc((char) 0x623, (char) 0xfe84, (char) 0xfe83,
                    (char) 0xfe84, (char) 0xfe83),
            new struc((char) 0x622, (char) 0xfe82, (char) 0xfe81,
                    (char) 0xfe82, (char) 0xfe81),
            new struc((char) 0x625, (char) 0xfe88, (char) 0xfe87,
                    (char) 0xfe88, (char) 0xfe87),
            new struc((char) 0x67e, (char) 0xfb57, (char) 0xfb58,
                    (char) 0xfb59, (char) 0xfb56), // peh
            new struc((char) 0x686, (char) 0xfb7b, (char) 0xfb7c,
                    (char) 0xfb7d, (char) 0xfb7a), // cheh
            new struc((char) 0x698, (char) 0xfb8b, (char) 0xfb8a,
                    (char) 0xfb8b, (char) 0xfb8a), // jeh
            new struc((char) 0x6a9, (char) 0xfb8f, (char) 0xfb90,
                    (char) 0xfb91, (char) 0xfb8e), // keheh
            new struc((char) 0x6af, (char) 0xfb93, (char) 0xfb94,
                    (char) 0xfb95, (char) 0xfb92), // gaf
            // new struc((char) 0x6cc, (char) 0xfbfd, (char) 0xfbfe,
            // (char) 0xfbff, (char) 0xfbfc), // Farsi yeh
            new struc((char) 0x6cc, (char) 0xfbfd, (char) 0xfef3,
                    (char) 0xfef4, (char) 0xfbfc), // Arabic yeh
            new struc((char) 0x6c0, (char) 0xfba5, (char) 0xfba4,
                    (char) 0xfba5, (char) 0xfba4) // heh with yeh
    };

    static struc[] arrStrucWoosim = {
            new struc((char) 0x630, (char) 0xb5, (char) 0x82, (char) 0xb5,
                    (char) 0x82),
            new struc((char) 0x62f, (char) 0xb4, (char) 0x81, (char) 0xb4,
                    (char) 0x81),
            new struc((char) 0x62c, (char) 0x9b, (char) 0xb1, (char) 0xf9,
                    (char) 0xbf),
            new struc((char) 0x62d, (char) 0x9c, (char) 0xb2, (char) 0xfa,
                    (char) 0xc0),
            new struc((char) 0x62e, (char) 0x9d, (char) 0xb3, (char) 0xfe,
                    (char) 0xc1),
            new struc((char) 0x647, (char) 0xac, (char) 0xe4, (char) 0x93,
                    (char) 0xd5),
            new struc((char) 0x639, (char) 0xc9, (char) 0xd3, (char) 0x8b,
                    (char) 0xa4),
            new struc((char) 0x63a, (char) 0xca, (char) 0xdd, (char) 0x8c,
                    (char) 0xa5),
            new struc((char) 0x641, (char) 0xa6, (char) 0xde, (char) 0x8d,
                    (char) 0xcc),
            new struc((char) 0x642, (char) 0xa7, (char) 0xdf, (char) 0x8e,
                    (char) 0xce),
            new struc((char) 0x62b, (char) 0xbd, (char) 0xaf, (char) 0xea,
                    (char) 0x99),
            new struc((char) 0x635, (char) 0xc4, (char) 0xc8, (char) 0x87,
                    (char) 0xa0),
            new struc((char) 0x636, (char) 0xc5, (char) 0xcb, (char) 0x88,
                    (char) 0xa1),
            new struc((char) 0x637, (char) 0xc6, (char) 0xcd, (char) 0xcd,
                    (char) 0xa2),
            new struc((char) 0x643, (char) 0xcf, (char) 0xe0, (char) 0x8f,
                    (char) 0xa8),
            new struc((char) 0x645, (char) 0xd2, (char) 0xe2, (char) 0x91,
                    (char) 0xaa),
            new struc((char) 0x646, (char) 0xd4, (char) 0xe3, (char) 0x92,
                    (char) 0xab),
            new struc((char) 0x62a, (char) 0xbd, (char) 0xaf, (char) 0xea,
                    (char) 0x99),
            new struc((char) 0x627, (char) 0xbb, (char) 0x80, (char) 0xbb,
                    (char) 0x80),
            new struc((char) 0x644, (char) 0xd1, (char) 0xe1, (char) 0x90,
                    (char) 0xa9),
            new struc((char) 0x628, (char) 0xbc, (char) 0xae, (char) 0xe9,
                    (char) 0x98),
            new struc((char) 0x64a, (char) 0xdc, (char) 0xe6, (char) 0x95,
                    (char) 0xdc),
            new struc((char) 0x633, (char) 0xc2, (char) 0xb8, (char) 0xb8,
                    (char) 0x9e),
            new struc((char) 0x634, (char) 0xc3, (char) 0xb9, (char) 0xb9,
                    (char) 0x9f),
            new struc((char) 0x638, (char) 0xc7, (char) 0xcd, (char) 0xcd,
                    (char) 0xc7),
            new struc((char) 0x632, (char) 0xb7, (char) 0xb7, (char) 0xb7,
                    (char) 0xb7),
            new struc((char) 0x648, (char) 0x94, (char) 0x94, (char) 0x94,
                    (char) 0x94),
            new struc((char) 0x629, (char) 0xda, (char) 0xda, (char) 0xda,
                    (char) 0xda),
            new struc((char) 0x649, (char) 0xdc, (char) 0xe6, (char) 0x95,
                    (char) 0xdc),
            new struc((char) 0x631, (char) 0xb6, (char) 0xb6, (char) 0xb6,
                    (char) 0xb6),
            new struc((char) 0x624, (char) 0xe7, (char) 0xe7, (char) 0xe7,
                    (char) 0xe7),
            new struc((char) 0x621, (char) 0xba, (char) 0xba, (char) 0xba,
                    (char) 0xba),
            new struc((char) 0x626, (char) 0xd7, (char) 0xe8, (char) 0x97,
                    (char) 0xd7),
            new struc((char) 0x623, (char) 0x80, (char) 0x80, (char) 0x80,
                    (char) 0x80),
            new struc((char) 0x622, (char) 0x80, (char) 0x80, (char) 0x80,
                    (char) 0x80),
            new struc((char) 0x625, (char) 0x80, (char) 0x80, (char) 0x80,
                    (char) 0x80),
            new struc((char) 0x67e, (char) 0xbc, (char) 0xae, (char) 0xe9,
                    (char) 0x98), // peh
            new struc((char) 0x686, (char) 0x9b, (char) 0xb1, (char) 0xf9,
                    (char) 0xbf), // cheh
            new struc((char) 0x698, (char) 0xb7, (char) 0xb7, (char) 0xb7,
                    (char) 0xb7), // jeh
            new struc((char) 0x6a9, (char) 0xcf, (char) 0xe0, (char) 0x8f,
                    (char) 0xa8), // keheh
            new struc((char) 0x6af, (char) 0xcf, (char) 0xe0, (char) 0x8f,
                    (char) 0xa8), // gaf
            new struc((char) 0x6cc, (char) 0xdc, (char) 0xe6, (char) 0x95,
                    (char) 0xdc), // yeh
            new struc((char) 0x6c0, (char) 0xac, (char) 0xe4, (char) 0x93,
                    (char) 0xd5) // heh with yeh
    };

    private static final int N_DISTINCT_CHARACTERS = 43;

    private static final String ArabicReverse(String s) {
        try {
            String Out = "", rev;
            s = MakeReverse(s);
            char[] chs = new char[s.length()];
            chs = s.toCharArray();
            int i = 0;
            while (i < s.length()) {
                if ((chs[i] >= '0' && chs[i] <= '9')) // isDigit(s[i]) ?
                {
                    rev = "";
                    while (i < s.length()
                            && ((chs[i] >= '0' && chs[i] <= '9') || chs[i] == '/')) // isDigit(s[i])
                                                                                    // ?
                    {
                        rev = rev + chs[i];
                        ++i;
                    }
                    rev = MakeReverse(rev);
                    Out = Out + rev;
                } else {
                    Out = Out + chs[i];
                    ++i;
                }
            }
            s = Out;
        } catch (Exception ex) {
            // throw new Exception(
            // "An exception has occurred in ArabicReverse function.\\n"
            // + ex.getMessage());
        }
        return s;
    }

    private static final boolean isFromTheSet1(/* WCHAR */char ch) {
        char[] theSet1 = new char[] { (char) 0x62c, (char) 0x62d, (char) 0x62e,
                (char) 0x647, (char) 0x639, (char) 0x63a, (char) 0x641,
                (char) 0x642, (char) 0x62b, (char) 0x635, (char) 0x636,
                (char) 0x637, (char) 0x643, (char) 0x645, (char) 0x646,
                (char) 0x62a, (char) 0x644, (char) 0x628, (char) 0x64a,
                (char) 0x633, (char) 0x634, (char) 0x638, (char) 0x67e,
                (char) 0x686, (char) 0x6a9, (char) 0x6af, (char) 0x6cc,
                (char) 0x626 };
        int i = 0;
        while (i < 28) {
            if (ch == theSet1[i])
                return true;
            ++i;
        }
        return false;
    }

    private static final boolean isFromTheSet2(/* WCHAR */char ch) {
        char[] theSet2 = new char[] { (char) 0x627, (char) 0x623, (char) 0x625,
                (char) 0x622, (char) 0x62f, (char) 0x630, (char) 0x631,
                (char) 0x632, (char) 0x648, (char) 0x624, (char) 0x629,
                (char) 0x649, (char) 0x698, (char) 0x6c0 };
        int i = 0;
        while (i < 14) {
            if (ch == theSet2[i])
                return true;
            ++i;
        }
        return false;
    }

    private static final String MakeReverse(String text) {
        String Result = "";
        char[] Ctext = new char[text.length()];
        Ctext = text.toCharArray();
        for (int i = (text.length()) - 1; i >= 0; i--) {
            Result += Ctext[i];
        }
        return Result;
    }

    public static final String ConvertBackToRealFarsi(String In) {

        if (!isFarsiConversionNeeded) {
            return In;
        }

        String strOut = "";
        StringBuilder strBuilder = new StringBuilder("");
        int i = 0;
        int j = 0;
        char[] chIn = new char[In.length()];
        chIn = In.toCharArray();

        for (i = 0; i < In.length(); i++) {
            boolean found = false;
            for (j = 0; j < arrStruc.length; j++) {
                if (chIn[i] == arrStruc[j].midGlyph ||
                        chIn[i] == arrStruc[j].iniGlyph ||
                        chIn[i] == arrStruc[j].endGlyph ||
                        chIn[i] == arrStruc[j].isoGlyph) {
                    strBuilder.append(arrStruc[j].character);
                    found = true;
                    break;
                }
            }
            if (!found)
                strBuilder.append(chIn[i]);
        }

        strOut = strBuilder.toString();
    strOut = strOut.replace(szLa, "لا");
    strOut = strOut.replace(szLaStick, "لا");

        return strOut;
    }

    public static final String Convert(String In) {

        if (!isFarsiConversionNeeded) {
            return In;
        }

        if (In == null) {
            return "";
        }

        boolean linkBefore, linkAfter;
        String Out = In;
        char[] chOut = new char[Out.length()];
        chOut = Out.toCharArray();
        char[] chIn = new char[In.length()];
        chIn = In.toCharArray();

        for (int i = 0; i < In.length(); i++) {
            /* WCHAR */
            char ch = chIn[i];
            if ((ch >= (char) 0x0621 && ch <= (char) 0x064a)
                    || (ch == (char) 0x067e) || (ch == (char) 0x0686)
                    || (ch == (char) 0x0698) || (ch == (char) 0x06a9)
                    || (ch == (char) 0x06af) || (ch == (char) 0x06cc)
                    || (ch == (char) 0x06c0)) // is a Farsi character?
            {
                int idx = 0;
                while (idx < N_DISTINCT_CHARACTERS) {
                    if (arrStruc[idx].character == chIn[i])
                        break;
                    ++idx;
                }

                if (i == In.length() - 1)
                    linkAfter = false;
                else
                    linkAfter = (isFromTheSet1(chIn[i + 1]) || isFromTheSet2(chIn[i + 1]));
                if (i == 0)
                    linkBefore = false;
                else
                    linkBefore = isFromTheSet1(chIn[i - 1]);
                if (idx < N_DISTINCT_CHARACTERS) {
                    if (linkBefore && linkAfter)
                        chOut[i] = arrStruc[idx].midGlyph;
                    if (linkBefore && !linkAfter)
                        chOut[i] = arrStruc[idx].endGlyph;
                    if (!linkBefore && linkAfter)
                        chOut[i] = arrStruc[idx].iniGlyph;
                    if (!linkBefore && !linkAfter)
                        chOut[i] = arrStruc[idx].isoGlyph;
                } else {
                    chOut[i] = chIn[i];
                }
            } else {
                chOut[i] = chIn[i];
            }
        }
        Out = "";
        for (int j = 0; j < chOut.length; j++)
            Out += chOut[j];
        // Out = ArabicReverse(Out);

        Out = Out.replace((char) 0x200c, ' '); // Change NO SPACE to SPACE

        Out = Out.replace(szLamAndAlef, szLa); // Join 'Lam' and 'Alef' and
                                                // make 'La'
        Out = Out.replace(szLamStickAndAlef, szLaStick); // Join 'Lam Stick'
                                                            // and 'Alef'
                                                            // and make 'La
                                                            // Stick'

        return reorderWords(Out);

    }

    private final static String reorderWords(String strIn)
    {

        final int ST_RTL = 0;
        final int ST_LTR = 1;

        String strOut = "";
        String prevWord = "";
        int state = ST_RTL;
        char[] arr = strIn.toCharArray();
        int i = 0;
        while (i < arr.length) {
            if (charIsLTR(arr[i]) && state != ST_LTR)
            {
                // state changed to LTR
                state = ST_LTR;
                strOut = prevWord + strOut;
                prevWord = "";
                prevWord += arr[i];
            }
            else if (charIsRTL(arr[i]) && state != ST_RTL)
            {
                // state changed to RTL
                state = ST_RTL;
                strOut = prevWord + strOut;
                prevWord = "";
                prevWord += arr[i];
            }
            else
            {
                // state is not changed
                prevWord += arr[i];
            }
            i++;
        }

        strOut = prevWord + strOut;

        return strOut;

    }

    private final static boolean charIsLTR(char ch)
    {
        return (ch >= (char) 65 & ch <= (char) 122)
                |
                Character.isDigit(ch);
    }

    private final static boolean charIsRTL(char ch)
    {
        return ch >= (char) 0x0621;
    }   

    private static Typeface typeface;

    public static final Typeface GetFarsiFont(Context context) {
        if (typeface == null) {
            typeface = Typeface.createFromAsset(context.getAssets(),
                    // "DroidSansFallback.ttf");
                    "TAHOMA.TTF");
        }
        return typeface;
    }
}

3) And in your Activity:

Typeface tf = Farsi.GetFarsiFont(this);
MyTextView.setTypeface(tf);
MyTextView.setText(Farsi.Convert("سلام"));

enjoy!

my reference for Farsi class: Arabizer

这篇关于如何支持阿拉伯文字的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆