如何在自定义Android键盘中开发NEXT按钮

时间:2015-04-08 10:17:31

标签: android android-activity

我正在使用扩展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 =&#34; actionNext&#34;,android:singleLine =&#34; true&#34; 在我的XML中。 但是我无法将光标发送到下一个EditText。 我需要改变什么?

1 个答案:

答案 0 :(得分:0)

我在我的活动中执行此操作,当我想在EditText上显示下一个按钮时出现:

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