java - 在 Android 键盘中每两个键输出一个字符

标签 java android locale android-softkeyboard non-latin

我正在为 Android 中的阿姆哈拉语设计自定义键盘,但以下内容适用于许多其他非英语语言。

两个或多个组合键转换为一个字符。因此,如果用户键入“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);
}

最佳答案

以下是我建议的一些更改,以提高效率

  1. 使用您自己的变量来跟踪编辑状态。在下面的代码中,我使用了 mComposing,在 StartInput 上清除它,并在进行新输入时更新它。
  2. 减少使用字符串。我已经使用自定义类替换了字符串并重组了您的转换映射。
  3. 使用撰写文本向用户更好地提示您正在做什么。

修改后的代码

private StringBuilder mComposing = new StringBuilder();
private static HashMap<Integer, CodeInfo> mCodeMap = new HashMap<Integer, CodeInfo>();

private static class CodeInfo {
   final Character mCode;
   final Map<Character, Character> mCombinedCharMap;

   CodeInfo(Character code, Map<Character, Character> combinedCharMap) {
       mCode = code;
       mCombinedCharMap = combinedCharMap;
   }
}

static {
    //reminder, do not input combinedCharMap as null

    mCodeMap.put(83, new CodeInfo(Character.valueOf((char)4656), new HashMap<Character, Character>());
    HashMap<Character, Character> combinedCharMap = new HashMap<Character, Character>();
    combinedCharMap.put(Character.valueOf('ሰ'), Character.valueOf((char)4659))
    mCodeMap.put(65, new CodeInfo(null, combinedCharMap);
}

@Override 
public void onStartInput(EditorInfo attribute, boolean restarting) {
    super.onStartInput(attribute, restarting);
    mComposing.setLength(0);


    //other codes you already have
}       

public void onKey(int primaryCode, int[] keyCodes) {
    InputConnection ic = getCurrentInputConnection();

    CodeInfo codeInfo = mCodeMap.get(primaryCode);
    Character output = null;
    if (codeInfo != null) {
        if (mComposing.length() > 0) {
            Character combinedOutput = codeInfo.mCombinedCharMap.get(mComposing.charAt(0));
            if (combinedOutput != null) {
                //the length is mComposing is expected to be 1 here
                mComposing.setCharAt(0, combinedOutput);
                ic.finishComposingText();
                ic.setComposingText(mComposing, 1);
                return;
            }
        }
        output = codeInfo.mCode;        
    }
    if (mComposing.length() > 0) {
       mComposing.setLength(0);
       ic.finishComposingText();
    }
    mComposing.append(output==null?(char)primaryCode:(char)output);
    ic.setComposingText(mComposing, 1);
}

关于java - 在 Android 键盘中每两个键输出一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32330858/

相关文章:

java - 是什么阻止了在 javafx 中更改 primaryStage.initStyle()?

java - 使用 VideoView 时获取 java.io.FileNotFoundException

android - 如何在 Android Studio 中的文件中搜索方法

java - 当按下设备上或底部栏上的后退按钮时,不会调用 onBackPressed

php - Symfony2 语言环境检测和翻译

c++ - 如何在 C++ 中处理 ifstream、cout 等的多个语言环境

r - 如何在 Ubuntu 中全局更改 R 语言环境?

JAR 中捆绑的 Java 执行 Shell 脚本

java - 为什么 -1 零填充右移 1=2147483647 对于 Java 中的整数?

java - Android Studio 2.3 Signature Step Verification v1(Jar Signature), v2(Full Apk Signature) 禁用