我刚刚在Android Studio中创建了一个简单的键盘应用程序,现在我想添加一些功能,例如拼写检查器。 我已经在这里找到了如何编写拼写检查器的解决方案:“ https://github.com/WillSen/spellchecker-autocorrect/blob/master/index.js” 但是现在,由于这是我的第一个Android-Studio项目,因此在将其应用于我的项目时遇到了一些问题: 如何读出用户按下的键? 如何将带有文字的数据库连接到Android Studio并使其离线可用? 如何在键上的XML文件中显示建议的单词? 我当前的主要功能如下:
import android.app.Service;
import android.content.Intent;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.media.AudioManager;
import android.os.IBinder;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputConnection;
public class OrthKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {
private KeyboardView kv;
private Keyboard keyboard;
private boolean isCaps = false;
//Press Ctrl+O
@Override
public View onCreateInputView() {
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard,null);
keyboard = new Keyboard(this,R.xml.qwerty);
kv.setKeyboard(keyboard);
kv.setOnKeyboardActionListener(this);
return kv;
}
@Override
public void onPress(int i) {
}
@Override
public void onRelease(int i) {
}
@Override
public void onKey(int i, int[] ints) {
InputConnection ic = getCurrentInputConnection();
playClick(i);
switch (i)
{
case Keyboard.KEYCODE_DELETE:
ic.deleteSurroundingText(1,0);
break;
case Keyboard.KEYCODE_SHIFT:
isCaps = !isCaps;
keyboard.setShifted(isCaps);
kv.invalidateAllKeys();
break;
case Keyboard.KEYCODE_DONE:
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_ENTER));
break;
default:
char code = (char)i;
if(Character.isLetter(code) && isCaps)
code = Character.toUpperCase(code);
ic.commitText(String.valueOf(code),1);
}
}
private void playClick(int i) {
AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
switch(i)
{
case 32:
am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
break;
case Keyboard.KEYCODE_DONE:
case 10:
am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
break;
case Keyboard.KEYCODE_DELETE:
am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
break;
default: am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
}
}
@Override
public void onText(CharSequence charSequence) {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeDown() {
}
@Override
public void swipeUp() {
}
}
我的XML这样:
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="60dp">
<Row>
<Key android:keyLabel="1"
android:keyEdgeFlags="left"
android:codes="49"/>
<Key android:keyLabel="2"
android:codes="50"/>
<Key android:keyLabel="3"
android:codes="51"/>
<Key android:keyLabel="4"
android:codes="52"/>
<Key android:keyLabel="5"
android:codes="53"/>
<Key android:keyLabel="6"
android:codes="54"/>
<Key android:keyLabel="7"
android:codes="55"/>
<Key android:keyLabel="8"
android:codes="56"/>
<Key android:keyLabel="9"
android:codes="57"/>
<Key android:keyLabel="0"
android:keyEdgeFlags="right"
android:codes="48"/>
</Row>
<Row>
<Key android:keyLabel="q" android:keyEdgeFlags="left" android:codes="113"/>
<Key android:keyLabel="w" android:codes="119"/>
<Key android:keyLabel="e" android:codes="101"/>
<Key android:keyLabel="r" android:codes="114"/>
<Key android:keyLabel="t" android:codes="116"/>
<Key android:keyLabel="y" android:codes="121"/>
<Key android:keyLabel="u" android:codes="117"/>
<Key android:keyLabel="i" android:codes="105"/>
<Key android:keyLabel="o" android:codes="111"/>
<Key android:keyLabel="p" android:keyEdgeFlags="right" android:codes="112"/>
</Row>
<Row>
<Key android:keyLabel="a" android:keyEdgeFlags="left" android:codes="97"/>
<Key android:keyLabel="s" android:codes="115"/>
<Key android:keyLabel="d" android:codes="100"/>
<Key android:keyLabel="f" android:codes="102"/>
<Key android:keyLabel="g" android:codes="103"/>
<Key android:keyLabel="h" android:codes="104"/>
<Key android:keyLabel="j" android:codes="106"/>
<Key android:keyLabel="k" android:codes="107"/>
<Key android:keyLabel="l" android:codes="108"/>
<Key android:keyLabel="\# \@" android:keyEdgeFlags="right" android:codes="35,64"/>
</Row>
<Row>
<Key android:keyLabel="CAPS" android:keyEdgeFlags="left" android:codes="-1"/>
<Key android:keyLabel="z" android:codes="122"/>
<Key android:keyLabel="x" android:codes="120"/>
<Key android:keyLabel="c" android:codes="99"/>
<Key android:keyLabel="v" android:codes="118"/>
<Key android:keyLabel="b" android:codes="98"/>
<Key android:keyLabel="n" android:codes="110"/>
<Key android:keyLabel="m" android:codes="109"/>
<Key android:keyLabel="." android:codes="46"/>
<Key android:keyLabel="\? ! :" android:keyEdgeFlags="right" android:codes="63,33,58"/>
</Row>
<Row android:rowEdgeFlags="bottom">
<Key android:keyLabel="," android:keyWidth="10%p" android:keyEdgeFlags="left" android:codes="44"/>
<Key android:keyLabel="/" android:keyWidth="10%p" android:codes="47"/>
<Key android:keyLabel="SPACE" android:keyWidth="40%p" android:isRepeatable="true" android:codes="32"/>
<Key android:keyLabel="DEL" android:keyWidth="20%p" android:isRepeatable="true" android:codes="-5"/>
<Key android:keyLabel="DONE" android:keyWidth="20%p" android:keyEdgeFlags="right" android:codes="-4"/>
</Row>
</Keyboard>