代码方式,一般是放在一个Utils.java作为公共方法
/// add by xxx.zhou for ArabicRTL support 20141024 begin
public static boolean isContainEG_IR(String str) {
if (str == null || str == "")
return false;
String rtl[] = new String[4];
rtl[0] = "[\u0600-\u06ff]"; // Arbic
rtl[1] = "[\u0750-\u077f]"; // Arbic Supplement
rtl[2] = "[\ufb50-\ufdff]"; // Arabic PresentationForms A
rtl[3] = "[\ufe70-\ufeff]"; // Arabic PresentationForms B
boolean isContained = false;
for (String s : rtl) {
Pattern p = Pattern.compile(s);
Matcher m = p.matcher(str);
if (m.find()) {
isContained = true;
break; // I've found out contain arabic and returns
}
}
return isContained;
}
//判断是否是一个号码,常用
public static boolean isPurePhoneNumber(String str) {
if (str == null)
return false; // allan add
char chs[] = str.toCharArray();
boolean flag = true;
for (char c : chs) {
if (c != '+' && !(c >= '0' && c <= '9') && c != '*' && c != ' '
&& c != ',') {
flag = false;
break;
}
}
return flag;
}
//设置文字的显示方向,一般此方法前判断一下isPurePhoneNumber()
public static String ChangeTextForRTL(String orgin, int type) {
// phone is RTL suport
if (type == 1) {
return '\u202D' + orgin + '\u202C';
} else {
return '\u202A' + orgin + '\u202C';
}
}
public static void SetTextDIRECTION(CharSequence temp, TextView showtextview) {
if (showtextview == null)
return;
String tempstrings = temp.toString();
if (!tempstrings.equals("")) {
boolean iscontain = isContainEG_IR(tempstrings);
if (iscontain) {
boolean isfirst = isFirstCharEnglish(tempstrings);
if (isfirst) {
showtextview.setTextDirection(android.view.View.TEXT_DIRECTION_LTR);
} else {
showtextview.setTextDirection(android.view.View.TEXT_DIRECTION_RTL);
}
} else {
showtextview.setTextDirection(android.view.View.TEXT_DIRECTION_LTR);
}
} else {
if (isLanguageEnvRTL()) {
showtextview.setTextDirection(android.view.View.TEXT_DIRECTION_RTL);
} else {
showtextview.setTextDirection(android.view.View.TEXT_DIRECTION_LTR);
}
}
}
public static boolean isLanguageEnvRTL() {
java.util.Locale l = java.util.Locale.getDefault();
String language = l.getLanguage();
if (language.equals("ar") || language.equals("iw") || language.equals("fa")) {
return true;
}
return false;
}
public static boolean isFirstCharEnglish(String str) {
char chs[] = str.toCharArray();
char c = ' ';
for (int i = 0; i < chs.length; i++) {
if (chs[i] != ' ') {
c = chs[i];
break;
}
}
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
return true;
}
return false;
}
/// add by xxx.zhou for ArabicRTL support 20141024 end
//add by xxx.li for PR884764 20150122 START
public static boolean isRuLanguage(Context context){
boolean mIsRussian = "ru".equals(context.getResources().getConfiguration().locale.getLanguage());
boolean mIsLastMatch = SystemProperties.getBoolean("ro.def.TelephonyProvider.match", false);
return mIsRussian && mIsLastMatch;
}
public static int getConfigPhnumMinMatch(){
int mMinMatch = Integer.valueOf(SystemProperties.get("ro_config_phnum_significant_len", "7"));
return mMinMatch;
}
//add by xxx.li for PR884764 20150122 END
xml设置
1、strings.xml
<!-- file values-ar/strings.xml -->
<string name="send_msg_to">استورد\u202d%s\u202cتور</string>
<string name="send_msg_to">\u200fsend <xliff:g id="number">\u200f%1$d</xliff:g>استورد</string>
2、布局文件TextView
1. 在你的应用程序声明文件(manifest)里声明开启RTL mirroring的支持。具体做法是:在manifest.xml声明文件的元素中,添加 android:supportsRtl=”true”
- 修改应用程序中所有的“left/right”布局属性,改为对应的”start/end”布局
1)如果你的应用程序是针对Android 4.2目标平台(应用的targetSdkVersion或者minSdkVersion是17或者更高), 那么你就应当用“start”和“end”替换原来的“left”和“right”。例如,android:paddingLeft应当被替换为android:paddingStart。
2) 如果你想让你的应用程序与Android 4.2之前的版本保持兼容(也就是与targetSdkVersion或者minSdkVersion为16或者更早的版本),那么你应当既加上“start”和“end”,又加上“left”和“right”。例如,你应当同时写上:adnroid:paddingLeft和android:paddingStart。
<!--[BUGFIX]-Mod-BEGIN by (xxx.deng),10/27/2015,1103440, -->
<!--Arabic support,add android:textDirection="ltr" -->
<EditText
android:id="@+id/edit_container"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:textDirection="ltr" />
<!--[BUGFIX]-Mod-END by (xxx),1103440.-->