在Android键盘中每两个键输出一个字符 [英] Output one character per two keys in Android Keyboard

查看:204
本文介绍了在Android键盘中每两个键输出一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Android设计用于Amharic语言的自定义键盘,但是以下内容适用于许多其他非英语语言.

I am designing a custom keyboard for Amharic language in Android, but the following is applicable to many other non-English languages.

键的两个或多个组合转换为一个字符.因此,如果用户键入"S",键盘将输出ሰ" ...,如果他们跟随字母"A",则将ሰ"替换为ሳ".

Two or more combination of keys translate to one character. So, if the user types 'S', the keyboard will output 'ሰ'... and if they follow it with the letter 'A', the 'ሰ' is replaced with 'ሳ'.

我设法找到一种解决方法,如下所示,方法是查看光标之前的字符并对照地图对其进行检查.但是,我想知道是否有更简单,更清洁的解决方案.

I managed to get a solution, as below, working by looking at the character before the cursor and checking it against a Map. However, I was wondering whether there is a simpler and cleaner solution possible.

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);
}

推荐答案

这里我建议进行一些更改以使其更有效

Here are some changes I would suggest to make it more efficient

  1. 使用您自己的变量来跟踪编辑状态.在下面的代码中,我使用了mComposed,在onStartInput上将其清除,并在进行新输入时对其进行更新.
  2. 减少String的使用.我已经使用自定义类替换了Strings并重组了您的转化图.
  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);
}

这篇关于在Android键盘中每两个键输出一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆