2

我想为我的 android 开发一个自定义软键盘。我的问题是我想添加 2 个额外的按钮,它们可以执行任何我想要的操作。更准确地说,我想在我的键盘上粘贴用户名和密码按钮,所以每当我专注于编辑框并按下这些按钮时,相应的任务将是这样的:

 arguments.putCharSequence(AccessibilityNodeInfo
                    .ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "username");
            source.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);

或者

 arguments.putCharSequence(AccessibilityNodeInfo
                    .ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "password");
            source.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);

这是我的键盘示例 qwerty.xml 的一部分:

 <Row>
    <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
    <Key android:codes="50" android:keyLabel="2"/>
    <Key android:codes="51" android:keyLabel="3"/>
    <Key android:codes="52" android:keyLabel="4"/>
    <Key android:codes="53" android:keyLabel="5"/>
    <Key android:codes="54" android:keyLabel="6"/>
    <Key android:codes="55" android:keyLabel="7"/>
    <Key android:codes="56" android:keyLabel="8"/>
    <Key android:codes="57" android:keyLabel="9"/>
    <Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/>
</Row>

这是主要代码:

public class SimpleIME extends InputMethodService
    implements KeyboardView.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);
    }
}

我想做这样的事情

4

1 回答 1

0

首先,您可能不应该将此问题与辅助功能服务联系起来。这与无障碍服务无关。虽然此代码可以作为无障碍服务的一部分工作,但它取决于该服务是否开启。您想坚持输入服务 API 的领域。你真正想做的是批量编辑。当从字典中选择候选人时,这与预测键盘将运行的代码类型相同。因此,此代码是:

 InputConnection ic = getCurrentInputConnection();
 ic.beginBatchEdit();
 ic.commitText(text, cursorPositionAfterEdit);
 ic.endBatchEdit();
于 2015-11-10T03:27:08.630 回答