Android實戰技巧:為從右向左語言定義複雜字串,程式碼和xml設定
阿新 • • 發佈:2019-02-08
程式碼方式,一般是放在一個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.-->