问题描述
这听起来有点可疑,但是,即使我也不知道这是否可以在android系统中使用.要求是我需要在edittext中编写两种语言,一种是urdu,另一种是默认英语,现在的问题出在字体上,因为我必须使用urdu字体作为默认字体,所以我使用了setTypeface,如下所示:
This may sound a bit fishy but, even i dont know if this is possible in android or not. The requirement is i need to write two languages in the edittext, one urdu and the other is default english, now the problem is in the typeface, since i have to use the urdu typeface as a default, i used setTypeface like the following:
editText.setTypeface(englishFont);
但是问题是这种字体的英语不好,实际上它比平时要小,因此我不得不使用另一种英语字体,但这会产生问题,我从editText获取getText,检测到乌尔都语和英语字符,然后使用spannableStringBuilder相应地更改字体,如下所示:
But the problem is the english in this typeface is not good, in fact it is smaller than usual for this i have to use another typeface for English, but this creates the problem, i get the getText from the editText, detect the urdu and english characters and then use spannableStringBuilder to change the typeface accordingly as following:
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中的文本保持不变,字体不变.这可能吗?
But this does not solve the problem, the text in editText remains the same, no change in typeface. Is this possible or not??
推荐答案
在进行了一些研究和努力之后,我终于能够为此创建通用函数:
After doing some research and hardwork finally i was able to create a universal function for this:
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;
}
这篇关于Android:为一种EditText使用两种自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!