【问题标题】:Android Keyboard input predefined textAndroid 键盘输入预定义文本
【发布时间】:2016-01-09 18:54:10
【问题描述】:

我一直在关注this tutorial 制作自定义键盘。到目前为止一切顺利。

但是,我想知道,是否可以创建一个按钮来输入预定义的文本。例如。一个标有“姓名”的按钮,用于输入用户的姓名? 如果可能,我该怎么做?我进行了广泛的研究,但一无所获。

【问题讨论】:

    标签: android keyboard keyboard-shortcuts android-softkeyboard custom-keyboard


    【解决方案1】:

    是的,这是可能的。在 InputMethodService 的实现中定义新的键码

    private final static int NAME_CODE = -32; // value is absolutely random. The main requirement - there should not be coincidence with other codes
    

    然后在 onKey() 方法中检查它

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        InputConnection ic = getCurrentInputConnection();
            switch(primaryCode){
            .....
            case NAME_CODE:
                ic.commitText(name, name.length());
                break;
            .....
    

    然后,只需使用此代码从您的键盘规范(例如 qwerty.xml)和逻辑中绑定特殊键。

    <Key android:codes="-32" android:keyLabel="NAME" android:keyWidth="20%p" android:isRepeatable="true"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      相关资源
      最近更新 更多