Android:为一个EditText使用两个自定义字体

时间:2016-04-11 07:41:08

标签: java android mobile fonts typeface

这可能听起来有点可疑但是,即使我不知道这是否可能在Android或不。要求是我需要在edittext中编写两种语言,一种是urdu,另一种是默认的英语,现在问题出现在字体中,因为我必须使用urdu字体作为默认值,我使用setTypeface如下: / p>

editText.setTypeface(englishFont);

但问题是这个字体中的英文不好,实际上它比平时要小,我必须使用另一种英文字体,但是这会产生问题,我从editText中获取getText,检测到urdu和英文字符,然后使用spannableStringBuilder相应地更改字体,如下所示:

if(Arrays.asList(urduCodes).contains(Integer.toHexString(text.charAt(i)).toUpperCase()))
                {
                    System.out.println("different: "+text.charAt(i));
                    SpannableStringBuilder urduBLD;
                    urduBLD = new SpannableStringBuilder("");
                    urduBLD.append(edi.getText().charAt(i));
                    System.out.println("urduBLD: "+urduBLD);
                    urduBLD.setSpan(new StyleSpan(urduFont.getStyle()),urduBLD.length(),urduBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    stringBuilder.append(urduBLD);



                } else 
                {
                    System.out.println(text.charAt(i)+"-Code:"+(int)text.charAt(i)+"-"+Integer.toHexString(text.charAt(i)));
                    SpannableStringBuilder engBLD;
                    engBLD = new SpannableStringBuilder("");
                    engBLD.append(edi.getText().charAt(i));
                    System.out.println("engBLD: "+engBLD);
                    engBLD.setSpan(new StyleSpan(englishFont.getStyle()),engBLD.length(),engBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    stringBuilder.append(engBLD);


                }

但是这并没有解决问题,editText中的文本保持不变,字体没有变化。这有可能吗?

1 个答案:

答案 0 :(得分:0)

在做了一些研究和努力之后,我终于能够为此创建一个通用功能:

public SpannableStringBuilder setWord(String text)
    {
        tokenList = new ArrayList<String>();

        startIndext = 0;
        endIndex = 0;
        isEnglish = false;

        stringBuilder = new SpannableStringBuilder("");

        Log.i("CustomUrduKeyBoard","inside setWord text is: "+text+" stringBuilder: "+stringBuilder);
        if(text == null)
        {

        } else 
        {

            for (int i = 0; i < text.length(); i++) 
            {
//              System.out.println(text.charAt(i) + "-Code:" + (int) text.charAt(i)
//                      + "-" + Integer.toHexString(text.charAt(i)));
                if (text.charAt(i) < 255) 
                {
                    if (!isEnglish) 
                    {
                        endIndex = i;
                        String token = text.substring(startIndext, endIndex);

                        SpannableStringBuilder urduBLD;
                        urduBLD = new SpannableStringBuilder("");
                        urduBLD.append(token);

                        urduBLD.setSpan(new CustomTypefaceSpan("", urduFont),0,urduBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                        stringBuilder.append(urduBLD);
                        tokenList.add(urduBLD.toString());
                        startIndext = endIndex;
                    }
                    isEnglish = true;
                    // english
                    continue;
                } else 
                {
                    if (isEnglish) 
                    {
                        endIndex = i;
                        String token = text.substring(startIndext, endIndex);

                        SpannableStringBuilder engBLD;
                        engBLD = new SpannableStringBuilder("");
                        engBLD.append(token);

                        engBLD.setSpan(new CustomTypefaceSpan("", englishFont),0,engBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                        stringBuilder.append(engBLD);
                        tokenList.add(engBLD.toString());
                        startIndext = endIndex;
                    }
                    isEnglish = false;
                    // urdu
                    continue;
                }
            }

            String token = text.substring(startIndext, text.length());

            for (int i = 0; i < token.length(); i++) 
            {
                if (token.charAt(i) < 255) 
                {

                    SpannableStringBuilder urduBLD;
                    urduBLD = new SpannableStringBuilder("");
                    urduBLD.append(token);

                    urduBLD.setSpan(new CustomTypefaceSpan("", englishFont),0,urduBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    stringBuilder.append(urduBLD);
                    tokenList.add(urduBLD.toString());

                    break;
                } else
                {

                    SpannableStringBuilder engBLD;
                    engBLD = new SpannableStringBuilder("");
                    engBLD.append(token);

                    engBLD.setSpan(new CustomTypefaceSpan("", urduFont),0,engBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    stringBuilder.append(engBLD);
                    tokenList.add(engBLD.toString());

                    break;
                }
            }

        }

        return stringBuilder;
    }