質問

Below is the code I'm using to add an OnKeyboardActionListener to my KeyboardView. (For the sake of brevity, I've omitted the required overridden methods that I've left empty.)

keyboardView.setOnKeyboardActionListener(new OnKeyboardActionListener() {

    private void shiftOn(boolean on) {
        System.out.println("Shifting " + (on ? "on" : "off"));
        keyboard.setShifted(on);
        shiftKey.icon = keyboard.isShifted() ? shiftLockDrawable : shiftDrawable;
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {

        Editable editable = editText.getEditableText();
        int selectionStart = editText.getSelectionStart();

        if (primaryCode == SHIFT) {
            shiftOn(!keyboard.isShifted());
        } else {
            if (primaryCode == DELETE) {
                if (editable != null && selectionStart > 0) {
                    editable.delete(selectionStart - 1, selectionStart);
                }
            } else {
                editable.insert(selectionStart, Character.toString((char) primaryCode));
            }

            shiftOn(false);
        }
    }
});

The problem

When I press the shift key, everything goes as expected; both the key's icon and the state of "shiftedness" changes.

However, when I press any other key (which is supposed to turn shift off), the state of shiftedness changes to off, but the icon doesn't change to the unshifted version. I experience the same problem when using text instead of icons.

I've tried calling postInvalidate() on my KeyboardView but to no avail.

Here's a video that highlights my problem.

役に立ちましたか?

解決

I added the following code to the end of the shiftOn method:

keyboard.invalidateKey(SHIFT);

他のヒント

It seems to me that the redrawing of the shift key is attached to the actual input event. Can you, instead of changing the shiftKeyIcon simulate a shift key press in code? You just generate an additional shift key press event when another key is pressed.

I hope this helps you.

I think that there is a problem in shiftOn(). I don't exactly know what shiftLockDrawable and shiftDrawable do, but maybe the following code works

private void shiftOn(boolean on) {
    System.out.println("Shifting " + (on ? "on" : "off"));
    shiftKey.icon = (on ? shiftLockDrawable : shiftDrawable);
    keyboard.setShifted(on);
}

If this doesn't work or if this isn't what you want, could you maybe provide more information?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top