自作エディタで文字入力ができるようになった。しかし、エミュレータでうまく動かない。HTC Desireだとうまく入力できるのになー。もしかして、機種ごとに動作が違ったりして…それだとかなり困る。InputConnectionあたりで何かミスでもしたのかな…しかしソースコードがかなり汚くなってしまった。整理しておかないとな
private class TextInputConnection extends BaseInputConnection{ private TextInputConnection(View targetView, boolean fullEditor){ super(targetView, fullEditor); } @Override public boolean commitText(CharSequence text, int newCursorPosition){ boolean ret = super.commitText(text, newCursorPosition); commit = text.toString(); if(commit.length() == notDecision.length()){ notDecision = ""; }else if(notDecision.length() > 0){ notDecision = notDecision.substring(commit.length()); } document.insert(caret.getRow(), caret.getColumn(), commit); caret.move(caret.getColumn() + commit.length(), caret.getRow()); invalidate(); return ret; } @Override public boolean setComposingText(CharSequence text, int newCursorPosition){ boolean ret = super.setComposingText(text, newCursorPosition); notDecision = ""; if(text instanceof SpannableString){ notDecision = ((SpannableString)text).toString(); styleSet.clear(); SpannableString string = (SpannableString)text; CharacterStyle[] styles = string.getSpans(0, text.length(), CharacterStyle.class); for(CharacterStyle style : styles){ int start = string.getSpanStart(style); int end = string.getSpanEnd(style); styleSet.add(style, start, end); } } invalidate(); return ret; } @Override public boolean sendKeyEvent(KeyEvent event){ int keyCode = event.getKeyCode(); if(event.getAction() == KeyEvent.ACTION_DOWN){ if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){ caret.move(caret.getColumn(), caret.getRow() + 1); invalidate(); }else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){ if(caret.getColumn() == document.getLine(caret.getRow()).length()){ if(caret.getRow() < document.getLineCount() - 1){ caret.move(0, caret.getRow() + 1); } }else{ caret.move(caret.getColumn() + 1, caret.getRow()); } invalidate(); }else if(keyCode == KeyEvent.KEYCODE_DPAD_UP){ caret.move(caret.getColumn(), caret.getRow() - 1); invalidate(); }else if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){ if(caret.getColumn() == 0){ int row = caret.getRow() - 1; if(row > -1){ caret.move(document.getLine(row).length(), row); } }else{ caret.move(caret.getColumn() - 1, caret.getRow()); } invalidate(); }else if(keyCode == KeyEvent.KEYCODE_DEL){ int row = caret.getRow(); int column = caret.getColumn(); if(!(row == 0 && column == 0)){ caret.move(column - 1, row); if(caret.getColumn() == -1){ if(row > 0){ caret.move(document.getLine(row - 1).length(), row - 1); } } document.deleteCharAt(row, column - 1); } invalidate(); }else if(keyCode == KeyEvent.KEYCODE_ENTER){ document.insert(caret.getRow(), caret.getColumn(), '\n'); caret.move(0, caret.getRow() + 1); invalidate(); }else if(keyCode == KeyEvent.KEYCODE_SPACE){ document.insert(caret.getRow(), caret.getColumn(), ' '); caret.move(caret.getColumn() + 1, caret.getRow()); invalidate(); } } return super.sendKeyEvent(event); } }