我目前已经使用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;

}


我在键盘上有一个按钮,当用户按下该按钮时,我希望将其更改为“不同的键盘”,即切换到其他视图。我希望其他键盘上有表情符号。但是不知道如何进行此切换。

谁能给我一些提示,解决方案,或者可能是一些我可以阅读以理解这一点的文档?

最佳答案

您可以在onCreateInputView方法上为布局添加膨胀。


  @Override
      公共视图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);

10-07 20:04
查看更多