我正在Android中为Amharic语言设计自定义键盘,但以下内容适用于许多其他非英语语言。
两个或多个键组合转换为一个字符。因此,如果用户键入'S',键盘将输出'ሰ'...如果他们用字母'A'跟随它,则'ሰ'将替换为'ሳ'。
我设法得到一个解决方案,如下所示,通过查看光标前的字符并根据Map检查它。但是,我想知道是否有更简单,更清洁的解决方案。
public void onKey(int primaryCode, int[] keyCodes) {
InputConnection ic = getCurrentInputConnection();
HashMap<String, Integer> en_to_am = new HashMap<String, Integer>();
CharSequence pChar = ic.getTextBeforeCursor(1, 0);
int outKey = 0;
//build a hashmap of 'existing character' + 'new key code' = 'output key code'
en_to_am.put("83", 4656);
en_to_am.put("ሰ65", 4659);
try {
//see if config exists in hashmap for 'existing character' + 'new key code'
if (en_to_am.get(pChar.toString() + primaryCode) != null) {
outKey = en_to_am.get(pChar.toString() + primaryCode);
ic.deleteSurroundingText(1, 0);
} else {
//else just translate latin to amharic (ASCII 83 = ሰ)
if (en_to_am.get("" + primaryCode) != null) {
outKey = en_to_am.get("" + primaryCode);
} else {
//if no translation exists, just output the latin code
outKey = primaryCode;
}
}
} catch (Exception e) {
outKey = primaryCode;
}
char code = (char) outKey;
ic.commitText(String.valueOf(code), 1);
}
答案 0 :(得分:3)
以下是我建议的一些更改,以提高效率
这是修改后的代码
$scope.$watchCollection('gridOptions.selectedItems', function (newVal, oldVal) {
if ($scope.gridOptions.selectedItems.length > 5) {
for (var i = 0; i < $scope.importableFilesData.length; i++) {
if ($scope.importableFilesData[i] === newVal[0]) {
$scope.gridOptions.selectRow(i, false);
}
}
}
答案 1 :(得分:1)
在静态代码中初始化en_to_am
static private Map<String, Integer> en_to_am = new HashMap<String,Integer>;
static {
//build a hashmap of 'existing character' + 'new key code' = 'output key code'
en_to_am.put("83", 4656);
en_to_am.put("ሰ65", 4659);
}
略过试试。
public void onKey(int primaryCode) {
InputConnection ic = getCurrentInputConnection();
CharSequence pChar = ic.getTextBeforeCursor(1, 0);
Integer pairInt = en_to_am.get(pChar.toString() + primaryCode);
Integer singleInt = en_to_am.get(primaryCode.toString());
int outKey = primaryCode;
if (pairInt != null) {
try {
ic.deleteSurroundingText(1, 0);
outkey = pairInt;
}
}
else if (singleInt != null) {
outkey = singleInt;
}
ic.commitText((char) outkey).toString()), 1);
}