首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在自定义Android键盘中开发NEXT Button

如何在自定义Android键盘中开发NEXT Button
EN

Stack Overflow用户
提问于 2015-04-08 18:17:31
回答 1查看 882关注 0票数 0

我正在开发自定义键盘使用扩展InputMethodService和实现的OnKeyboardActionListener。我正在捕获一个类中的所有键事件,在这个类中,我将获得每个键的所有事件并执行操作。

代码语言:javascript
复制
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是:

代码语言:javascript
复制
<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>

一切都很好。但是我想使用键盘上的“下一步”按钮而不是“完成”按钮。我在我的XML中使用了android:imeOptions="actionNext",android:singleLine="true“。但我无法将光标发送到下一个EditText。我必须改变什么?

EN

回答 1

Stack Overflow用户

发布于 2015-04-08 18:25:09

当我想在EditText上显示下一步按钮时,我会在我的活动中执行此操作

代码语言:javascript
复制
        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;
            }
        });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29511892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档