Created
September 19, 2017 08:06
-
-
Save ssaurel/f44e55d8bac19560d6a95cb19d9ff11f to your computer and use it in GitHub Desktop.
InputMethodService implementation for a tutorial on the SSaurel's Channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ssaurel.mykeyboard; | |
import android.inputmethodservice.InputMethodService; | |
import android.inputmethodservice.Keyboard; | |
import android.inputmethodservice.KeyboardView; | |
import android.text.TextUtils; | |
import android.view.View; | |
import android.view.inputmethod.InputConnection; | |
public class MyInputMethodService extends InputMethodService implements KeyboardView.OnKeyboardActionListener { | |
@Override | |
public View onCreateInputView() { | |
KeyboardView keyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard_view, null); | |
Keyboard keyboard = new Keyboard(this, R.xml.number_pad); | |
keyboardView.setKeyboard(keyboard); | |
keyboardView.setOnKeyboardActionListener(this); | |
return keyboardView; | |
} | |
@Override | |
public void onPress(int i) { | |
} | |
@Override | |
public void onRelease(int i) { | |
} | |
@Override | |
public void onKey(int primatyCode, int[] keyCodes) { | |
InputConnection inputConnection = getCurrentInputConnection(); | |
if (inputConnection != null) { | |
switch(primatyCode) { | |
case Keyboard.KEYCODE_DELETE : | |
CharSequence selectedText = inputConnection.getSelectedText(0); | |
if (TextUtils.isEmpty(selectedText)) { | |
inputConnection.deleteSurroundingText(1, 0); | |
} else { | |
inputConnection.commitText("", 1); | |
} | |
break; | |
default : | |
char code = (char) primatyCode; | |
inputConnection.commitText(String.valueOf(code), 1); | |
} | |
} | |
} | |
@Override | |
public void onText(CharSequence charSequence) { | |
} | |
@Override | |
public void swipeLeft() { | |
} | |
@Override | |
public void swipeRight() { | |
} | |
@Override | |
public void swipeDown() { | |
} | |
@Override | |
public void swipeUp() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment