如何根据文本字段更改android自定义键盘的操作键?

时间:2015-03-12 05:32:37

标签: java android android-softkeyboard custom-keyboard

我正在开发一个Android键盘。我根据用户选择的文本字段尝试使用此方法更改自定义键盘操作键。但是这种方法不起作用。有没有其他方法可以更改操作键?

public class MyKeyboard extends InputMethodService implements
        OnKeyboardActionListener {

    @Override
    public void onStartInputView(EditorInfo info, boolean restarting) {
        super.onStartInputView(info, restarting);

        if(info.actionId == EditorInfo.IME_ACTION_DONE){
            //Action Done
        }else if(info.actionId == EditorInfo.IME_ACTION_SEARCH){
            //Action Search
        }else if(info.actionId == EditorInfo.IME_ACTION_UNSPECIFIED){
            //Action Unspecified
        }

        //more code here
    }

}

1 个答案:

答案 0 :(得分:2)

我在Keyboard类中使用过它。

    public class LatinKeyboard extends Keyboard {
         /**
         * This looks at the ime options given by the current editor, to set the
         * appropriate label on the keyboard's enter key (if it has one).
         */
        void setImeOptions(Resources res, int options) {

            switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
                case EditorInfo.IME_ACTION_GO:
                   //Action GO
                   break;
                case EditorInfo.IME_ACTION_NEXT:
                   //Action Next
                   break;
                case EditorInfo.IME_ACTION_SEARCH:
                    //Action Search
                    break;
                case EditorInfo.IME_ACTION_SEND:
                    //Action SEND
                    break;
                default:
                    //Action Default
                    break;
            }
        }
}

这里的选项是来自EditorInfo的imeOptions,我在onStartInput中调用了这个方法。