InputConnection是连接软键盘和View之间的通道
1、先初始化软键盘 InputMethodManager
input = (InputMethodManager) this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);2、创建自定义的InputConnection继承自BaseInputConnection,重载里头的commitText(Charsquence s)截获键盘输入的文字
public class InputView extends BaseInputConnection{
private CellEditView mCellEdit;
public InputView(View targetView, boolean fullEditor) {
super(targetView, fullEditor);
}
public InputView(CellEditView editView) {
super(editView, false);
mCellEdit = editView;
}
@Overridepublic boolean commitText(CharSequence text, int newCursorPosition) {
System.out.println("截获的文字 :"+text);
// return super.commitText(text, newCursorPosition); return true;
}
3、在View里重载onCreateInputConnetion,返回自定义InputConnetion对象
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
System.out.println("view");
return new InputView(this, false);
}
4、在View的 onTouchEvent 里方法里
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
this.setFocusableInTouchMode(true); //每次都要获取焦点才可以,我一直就是错在这里input.showSoftInput(this, 0); //弹出软键盘 return true;
}
原文:http://wenku.baidu.com/link?url=dIRilK-UTrzZBR0QPSqs0ydX6PsxJdi9_4CbmvqOSE0Tv1DD_nVVtrsC4WqsiAG0I58pFxEL1OhcLi8YrIlgryuQongoHT48i7yxfxurj-O