フリック入力が行えるキーボードを作成しています。
##現状、やりたいこと
以下はIMEのメインのコードです。
現状では「あ」を押すと「あ」が入力できます。
今後は「あ」を押した後フリックであ行が入力できる、一般的な日本語フリック入力を実装したいです。
public class InputIME extends InputMethodService implements KeyboardView.OnKeyboardActionListener{ private KeyboardView kv; private Keyboard keyboard; @Override public View onCreateInputView(){ kv = (KeyboardView)getLayoutInflater().inflate(R.layout.input_keyboard, null); keyboard = new Keyboard(this, R.xml.input_keyboard); kv.setKeyboard(keyboard); kv.setOnKeyboardActionListener(this); return kv; } @Override public void onPress(int primaryCode) { } @Override public void onRelease(int primaryCode) { } @Override public void onKey(int primaryCode, int[] keyCodes) { if(primaryCode == Keyboard.KEYCODE_DELETE){ keyDownUp(KeyEvent.KEYCODE_DEL); }else if(primaryCode == Keyboard.KEYCODE_SHIFT){ keyDownUp(KeyEvent.KEYCODE_SHIFT_RIGHT); }else if(primaryCode == Keyboard.KEYCODE_DONE){ keyDownUp(KeyEvent.KEYCODE_ENTER); }else if(primaryCode == Keyboard.EDGE_LEFT){ keyDownUp(KeyEvent.KEYCODE_DPAD_LEFT); }else if(primaryCode == Keyboard.EDGE_RIGHT){ keyDownUp(KeyEvent.KEYCODE_DPAD_RIGHT); }else if(primaryCode == InputCode.NONE){ getCurrentInputConnection().commitText("", 1); }else if(primaryCode == InputCode.A){ getCurrentInputConnection().commitText("あ", 1); }else if(primaryCode == InputCode.KA){ getCurrentInputConnection().commitText("か", 1); }else if(primaryCode == InputCode.SA){ getCurrentInputConnection().commitText("さ", 1); }else if(primaryCode == InputCode.TA){ getCurrentInputConnection().commitText("た", 1); }else if(primaryCode == InputCode.NA){ getCurrentInputConnection().commitText("な", 1); }else if(primaryCode == InputCode.HA){ getCurrentInputConnection().commitText("は", 1); }else if(primaryCode == InputCode.MA){ getCurrentInputConnection().commitText("ま", 1); }else if(primaryCode == InputCode.YA){ getCurrentInputConnection().commitText("や", 1); }else if(primaryCode == InputCode.RA){ getCurrentInputConnection().commitText("ら", 1); }else if(primaryCode == InputCode.WA){ getCurrentInputConnection().commitText("わ", 1); }else{ //文字の割り当てのあるKEYCODEの場合 getCurrentInputConnection().commitText(String.valueOf((char)primaryCode), 1); } } private void keyDownUp(int keyEventCode) { getCurrentInputConnection().sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyEventCode)); getCurrentInputConnection().sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyEventCode)); } @Override public boolean onKeyDown(int keyCode, KeyEvent event){ return super.onKeyDown(keyCode, event); } @Override public void onText(CharSequence text) { } @Override public void swipeLeft() { } @Override public void swipeRight() { } @Override public void swipeDown() { } @Override public void swipeUp() { } }
##問題点
まずタッチ座標を取得しようと以下のコードを書きました。
デバックすると最初にactivity_main(Hello worldとだけ書かれた画面)が開き、その画面上では座標を取得できました。
しかし、この画面を閉じてメモアプリなどで自作のIMEを開くと座標が取得できなくなってしまいます。
IME上で座標を取得するにはどうしたら良いでしょうか。
エラーなどははかれていません。
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onTouchEvent(MotionEvent event){ String action=""; switch(event.getAction()){ case MotionEvent.ACTION_DOWN: action ="ACTION_DOWN"; break; case MotionEvent.ACTION_UP: action="ACTION_UP"; break; case MotionEvent.ACTION_MOVE: action="ACTION_MOVE"; break; case MotionEvent.ACTION_CANCEL: action="ACTION_CANCEL"; break; } Log.v("MotionEvent","action=" + action + "," + "x="+String.valueOf(event.getX())+","+"y="+String.valueOf(event.getY())); return super.onTouchEvent(event); } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。