1
我喜歡從西里爾文到拉丁文飛信。例如,當用戶輸入西裏爾字母時,我喜歡將字母轉換爲拉丁文。下面是代碼:過濾方法被稱爲兩次android edittext上更改鍵盤語言
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = (EditText) findViewById(R.id.test);
InputFilter filter = new InputFilter() {
TransliterationHelper tr = new TransliterationHelper();
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if (tr.isAlphaCyrilic(source.toString())) {
String convertedString = tr.returnLatinForCyrilic(source.toString());
return convertedString.toUpperCase();
} else if (tr.isAlpha(source.toString()))
return source.toString().toUpperCase();
else
return "";
return null;
}
};
test.setFilters(new InputFilter[]{filter});
}
下面是isAlphaCyrilic功能:
public static boolean isAlphaCyrilic(String s) {
boolean isCyrilic = false;
for (char c : s.toCharArray()) {
if (Character.UnicodeBlock.of(c) == Character.UnicodeBlock.CYRILLIC) {
isCyrilic = true;
break;
}
}
return isCyrilic;
}
這裏是是字母
public static boolean isAlpha(String s) {
String pattern = "^[a-zA-Z ]*$";
if (s.matches(pattern)) {
return true;
}
return false;
}
功能returnLatinForCyrilic代碼,返回匹配的字符西裏爾字母:
public String returnLatinForCyrilic(String s) {
String strTranslated = cyrilicToLatinMap.get(s);
return strTranslated;
}
例如,我只輸入拉丁字母或西裏爾字母,一切正常,但是當我在拉丁語(我改變鍵盤語言)後再輸入西裏爾字母時,方法過濾器再次調用,而我不喜歡那樣。
有人有一些想法嗎?