Code example for InputConnection

Methods: beginBatchEditcommitTextendBatchEdit

0
    } 
 
    /** Helper function to commit any text being composed in to the editor. */ 
    private void commitTyped(InputConnection inputConnection) {
        if (mComposing.length() > 0) {
            inputConnection.commitText(mComposing, mComposing.length());
            mComposing.setLength(0);
            updateCandidates(); 
        } 
    } 
 
    /** Helper to update the shift state of our keyboard based on the initial 
     * editor state. */ 
    private void updateShiftKeyState(EditorInfo attr) {
        if(attr!=null && mInputView!=null && mQwertyKeyboard==mInputView.getKeyboard()){
            int caps=0;
            EditorInfo ei=getCurrentInputEditorInfo();
            if(ei!=null && ei.inputType!=EditorInfo.TYPE_NULL)
                caps=getCurrentInputConnection().getCursorCapsMode(attr.inputType);
            mInputView.setShifted(mCapsLock || caps!=0);
        } 
    } 
     
    /** Helper to determine if a given character code is alphabetic. */ 
    private boolean isAlphabet(int code){return Character.isLetter(code);}
     
    /** Helper to send a key down / key up pair to the current editor. */ 
    private void keyDownUp(int keyEventCode){
    	InputConnection ic=this.getCurrentInputConnection();
    	KeyEvent e=new KeyEvent(ACTION_DOWN, keyEventCode);
        ic.sendKeyEvent(e);
        e=new KeyEvent(ACTION_UP, keyEventCode);
        ic.sendKeyEvent(e);
    } 
     
    /** Helper to send a character to the editor as raw key events. */ 
    private void sendKey(int keyCode){
        switch (keyCode) {
            case '\n': 
                this.keyDownUp(KeyEvent.KEYCODE_ENTER);
                break; 
            default: 
                if (keyCode >= '0' && keyCode <= '9')
                    keyDownUp(keyCode - '0' + KeyEvent.KEYCODE_0);
                else 
                    getCurrentInputConnection().commitText(String.valueOf((char) keyCode), 1);
                break; 
        } 
    } 
 
    // Implementation of KeyboardViewListener 
 
    @Override public final void onKey(int primaryCode, int[] keyCodes){
        if(isWordSeparator(primaryCode)){
            // Handle separator 
            if(0<mComposing.length())
                commitTyped(getCurrentInputConnection()); 
             
            sendKey(primaryCode);
            updateShiftKeyState(getCurrentInputEditorInfo()); 
        }  
        else if(primaryCode==Keyboard.KEYCODE_DELETE)
            handleBackspace(); 
         
        else if (primaryCode == Keyboard.KEYCODE_SHIFT)
            handleShift(); 
         
        else if (primaryCode == Keyboard.KEYCODE_CANCEL) {
            handleClose(); 
            return; 
        }  
        else if (primaryCode == CCKeyboardView.KEYCODE_OPTIONS) {
            // Show a menu or something 
        }  
        else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE
                && mInputView != null) {
            Keyboard current = mInputView.getKeyboard();
            current = mQwertyKeyboard;
            mInputView.setKeyboard(current);
        }  
        else 
            handleCharacter(primaryCode, keyCodes);
 
    } 
 
    public final void onText(CharSequence text){
        InputConnection ic=getCurrentInputConnection();
        if(ic==null)
        	return; 
        ic.beginBatchEdit();
        if(0<mComposing.length())
            commitTyped(ic);
        ic.commitText(text, 0);
        ic.endBatchEdit();
        updateShiftKeyState(getCurrentInputEditorInfo()); 
    } 
 
    /** Update the list of available candidates from the current composing 
     * text.  This will need to be filled in by however you are determining 
Stop searching for code, let great code find you!  Add Codota to your java IDE