我是Android编程的新手,实际上是所有类型的编程;但我正在尝试将翻译器的免费代码从我找到的应用程序改为软键盘。我需要一些帮助,因为我确信我犯了这么大的错误。基本上是由于应用程序在启动时崩溃。
public class Keyboard_main extends InputMethodService implements KeyboardView.OnKeyboardActionListener {
public KeyboardView kv = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard, null);
private Keyboard keyboard = new Keyboard(this, R.xml.qwerty);
private boolean caps = false;
static final int KEYCODE_TRANSKEY = -1201;
如您所见,我有一个特定的KEYCODE:
static final int KEYCODE_TRANSKEY = -1201;
我稍后在我的Keyboard_main类中使用onKey():
@Override
public void onKey(int primaryCode, int[] keyCodes) {
InputConnection ic = getCurrentInputConnection();
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(60);
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;
case KEYCODE_TRANSKEY:
View popUpView = getLayoutInflater().inflate(R.layout.main_page, null);
PopupWindow popupWindow = new PopupWindow(popUpView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
popupWindow.showAtLocation(popUpView, Gravity.CENTER, 0, 0);
break;
default:
char code = (char) primaryCode;
if (Character.isLetter(code) && caps) {
code = Character.toUpperCase(code);
}
ic.commitText(String.valueOf(code), 1);
}}
我忘记提及我的其他活动必须是PopUp,因为它将成为翻译。
提前致谢,
ARNAUMONZÁLEZ