【问题标题】:How to develop NEXT Button in custom Android keyboard如何在自定义 Android 键盘中开发 NEXT 按钮
【发布时间】:2023-04-04 23:01:01
【问题描述】:

我正在通过扩展 InputMethodService 开发自定义键盘并实现 OnKeyboardActionListener。我在一个类中捕获所有键事件,在该类中我获取每个键的所有事件并执行操作。

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);                  
    }
}

我的 Querty.xml 是:

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="0px"
android:keyHeight="60dp"
android:keyWidth="10%p"
android:verticalGap="0px" >

<Row>
    <Key
        android:codes="81"
        android:keyEdgeFlags="left"
        android:keyLabel="Q" />
    <Key
        android:codes="87"
        android:keyLabel="W" />
    <Key
        android:codes="69"
        android:keyLabel="E" />
    <Key
        android:codes="82"
        android:keyLabel="R" />
    <Key
        android:codes="84"
        android:keyLabel="T" />
    <Key
        android:codes="89"
        android:keyLabel="Y" />
    <Key
        android:codes="85"
        android:keyLabel="U" />
    <Key
        android:codes="73"
        android:keyLabel="I" />
    <Key
        android:codes="79"
        android:keyLabel="O" />
    <Key
        android:codes="80"
        android:keyLabel="P" />

</Row>
<Row>
    <Key
        android:codes="65"
        android:keyEdgeFlags="left"
        android:keyLabel="A" />
    <Key
        android:codes="83"
        android:keyLabel="S" />
    <Key
        android:codes="68"
        android:keyLabel="D" />
    <Key
        android:codes="70"
        android:keyLabel="F" />
    <Key
        android:codes="71"
        android:keyLabel="G" />
    <Key
        android:codes="72"
        android:keyLabel="H" />
    <Key
        android:codes="74"
        android:keyLabel="J" />
    <Key
        android:codes="75"
        android:keyLabel="K" />
    <Key
        android:codes="76"
        android:keyLabel="L" />

</Row>
<Row>
    <Key
        android:codes="-5"
        android:isRepeatable="true"
        android:keyEdgeFlags="left"
        android:keyLabel="DEL"
        android:keyWidth="20%p" />
    <Key
        android:codes="90"
        android:keyLabel="Z" />
    <Key
        android:codes="88"
        android:keyLabel="X" />
    <Key
        android:codes="67"
        android:keyLabel="C" />
    <Key
        android:codes="86"
        android:keyLabel="V" />
    <Key
        android:codes="66"
        android:keyLabel="B" />
    <Key
        android:codes="78"
        android:keyLabel="N" />
    <Key
        android:codes="77"
        android:keyLabel="M" />
    <Key
        android:codes="-4"
        android:isRepeatable="true"
        android:keyEdgeFlags="right"
        android:keyLabel="DONE"
        android:keyWidth="20%p" />
</Row>

一切正常。但我想在我的键盘上使用 NEXT 按钮而不是 DONE 按钮。 我用过 android:imeOptions="actionNext", android:singleLine="true" 在我的 XML 中。 但我无法将光标发送到下一个 EditText。 我必须改变什么?

【问题讨论】:

    标签: android android-activity


    【解决方案1】:

    我在我的 Activity 中执行此操作,当我希望在 EditText 上显示 NEXT 按钮时:

            courseCRN= (EditText) courseCRNLayout.findViewById(R.id.editText);
            View focusView = null;
            courseCRN.setImeActionLabel("NEXT",3);
            courseCRN.setPrivateImeOptions("actionUnspecified");
            courseCRN.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
                    if (id == 3 || id == EditorInfo.IME_NULL) {
                       //focus on next View
                         focusView = yourNextView;  
                         focusView.requestFocus();
                        return true;
                    }
                    return false;
                }
            });
    

    【讨论】:

      猜你喜欢
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      相关资源
      最近更新 更多