如何在点击密钥代码上添加另一个键盘

时间:2016-03-22 18:01:24

标签: android

我必须添加一个新的字符键盘,当前的一个是字母数字。如果我在键盘之间切换帮助会很棒。我准备好了键盘,但只剩下开关部分了。

以下是主要代码。

public class SimpleIME extends InputMethodService implements OnKeyboardActionListener {

  private KeyboardView kv;
  private Keyboard keyboard;

  private boolean caps = false;

  @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:
            char code = (char)primaryCode;
            if(Character.isLetter(code) && caps){
                code = Character.toUpperCase(code);
            }
            ic.commitText(String.valueOf(code),1);
      }
  }

  @Override
  public void onPress(int primaryCode) {
  }

  @Override
  public void onRelease(int primaryCode) {
  }

  @Override
  public void onText(CharSequence text) {
  }

  @Override
  public void swipeDown() {
  }

  @Override
  public void swipeLeft() {
  }

  @Override
  public void swipeRight() {
  }

  @Override
  public void swipeUp() {
  }

  @Override
  public View onCreateInputView() {
    kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
    keyboard = new Keyboard(this, R.xml.qwerty);
    kv.setKeyboard(keyboard);
    kv.setOnKeyboardActionListener(this);
    return kv;
  }

  private void playClick(int keyCode) {
    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    switch (keyCode) {
        case 32:
            am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
            break;
        case Keyboard.KEYCODE_DONE:
        case 10:
            am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
            break;
        case Keyboard.KEYCODE_DELETE:
            am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
            break;
        default:
            am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我知道这个问题是在一年前问过的,但我会给你答案,因为它可能对其他人有所帮助。

在以下代码下面: " case Keyboard.KEYCODE_DONE:      ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_ENTER));             打破;"

把这段代码: "案例-201:      keyboard = new Keyboard(this,R.xml.symbols);      kv.setKeyboard(键盘);      打破;"

然后转到qwerty.xml文件,并为代码' -201'分配一个按钮。当用户按下此按钮时,键盘视图将切换为显示' symbols.xml'