通过cordova+contentEditable开发hybird app时,输入框中backspace无反应的问题

近日在通过cordova+contentEditable开发富文本编辑器,但是在虚拟机中测试的好好的到了真机中点击backspace却不能删除图片,必须点一下换行键,再把光标移回上面图片后面才可以。解决方法如下:

在org.apache.cordova包中创建CordovaInputConnection类

package org.apache.cordova;

import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.BaseInputConnection;
public class CordovaInputConnection extends BaseInputConnection{

	 public CordovaInputConnection(View targetView, boolean fullEditor) {
	        super(targetView, fullEditor);
	    }

	    @Override
	    public boolean deleteSurroundingText(int beforeLength, int afterLength) {       
	        // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
	        if (beforeLength == 1 && afterLength == 0) {
	            // backspace
	            return super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
	                && super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
	        }

	        return super.deleteSurroundingText(beforeLength, afterLength);
	    }
	    
}


然后在cordovaWebView中重写onCreateInputConnecton方法

 @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        CordovaInputConnection connection = new CordovaInputConnection(this, false);

        return connection;
    }

就可以解决无效的问题


代码下载


最近发现使用contentEditable做文本编辑器会出现输入中文异常的问题,所以就改为native的方式实现,使用了一个开源的框架,见https://github.com/xmuSistone/android-animate-RichEditor

你可能感兴趣的:(android开发)