开发了软键盘 - 如何切换视图/服务?

时间:2014-12-26 13:09:41

标签: android image service keyboard emoticons

我目前使用InputMethodService开发了一个软键盘。

它目前创建了一个功能性的QWERTY键盘:

public class KeyboardIME extends InputMethodService
    implements KeyboardView.OnKeyboardActionListener {

private KeyboardView kbv;
private Keyboard keyboard;


@Override
public void onKey(int primaryCode, int[] keyCodes) {
    InputConnection ic = getCurrentInputConnection();
    playClick(primaryCode);
    switch(primaryCode)
    {
        case Keyboard.KEYCODE_DELETE :
            ic.deleteSurroundingText(1, 0);
            break;
        case Keyboard.KEYCODE_SHIFT:
            caps = !caps;
            keyboard.setShifted(caps);
            kv.invalidateAllKeys();
            break;
        case Keyboard.KEYCODE_DONE:
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,   
             KeyEvent.KEYCODE_ENTER));
            break;
        default:
            String codeText = (String) String.valueOf(primaryCode);
            char code = (char)primaryCode;

            if(Character.isLetter(code) )
            {
                if(caps)
                {
                    code = Character.toUpperCase(code);
                }
            }
            else if (Integer.valueOf(codeText)==3890)
            {
                System.out.println("Testing to see if I can make a switch here..");
                break;
            }
            else {
                ic.commitText(String.valueOf(code), 1);
                break;
            }
      }





      @Override
     public View onCreateInputView() {


      kbv = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard, null);
      keyboard = new Keyboard(this, R.xml.qwerty);
      kbv.setKeyboard(keyboard);
      kbv.setOnKeyboardActionListener(this);

      return kbv;

}

我在键盘上有一个按钮,当用户按下它时,我希望它改为一个不同的键盘" - 即切换到不同的视图。我希望这个其他键盘上有表情符号。但是不知道如何进行这种转换。

有人能为我提供一些技巧,解决方案或可能的一些文档,我可以通过阅读来理解这一点吗?

1 个答案:

答案 0 :(得分:1)

您可以在onCreateInputView方法上为布局充气。

  

@覆盖       public View onCreateInputView(){

    View view = getLayoutInflater().inflate(R.layout.activity_main, null);
    yourLayout = (LinearLayout) view.findViewById(R.id.layout);
    return view;


}

然后你可以创建更多的键盘并设置可见性。

keyboardNums

  

kv =(KeyboardView)getLayoutInflater()。inflate(R.layout.keyboard,   空);

            keyboard = new Keyboard(mContext, R.layout.nums);
            kv.setKeyboard(keyboard);
            kv.setOnKeyboardActionListener(onKeyboardActionListener);

键盘qwerty

  

kv2 =(KeyboardView)getLayoutInflater()。inflate(R.layout.keyboard,null);

            keyboard2 = new Keyboard(mContext, R.layout.qwerty);
            kv2.setKeyboard(keyboard);
            kv2.setOnKeyboardActionListener(onKeyboardActionListener);