Reputation: 3466
I like to convert on the fly letters from cyrillic to latin. For example when user enter cyrillic letter I like to convert letter to latin. Here is the code:
@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});
}
Here is isAlphaCyrilic function:
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;
}
Here is the code for isAlpha
public static boolean isAlpha(String s) {
String pattern = "^[a-zA-Z ]*$";
if (s.matches(pattern)) {
return true;
}
return false;
}
The function returnLatinForCyrilic, return matched character for cyrillic letter:
public String returnLatinForCyrilic(String s) {
String strTranslated = cyrilicToLatinMap.get(s);
return strTranslated;
}
For example I enter only latin letters or cyrillic letters everything works ok, but when I enter cyrillic letter after latin (I changed keyboard language) method filter called again, and I don't like that.
Does someone has some idea?
Upvotes: 1
Views: 668
Reputation: 3466
I put android:inputType="textNoSuggestions"
so the method filter was not called twice.
Upvotes: 2