通过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

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值