在Android 4.x上的Phonegap下,无法让退格在codemirror中工作?

Nis*_*isk 8 android input backspace android-webview cordova

对于我的应用程序,我需要一个基于Web的文本/代码编辑器,表现良好.

我正在尝试在Phonegap下使用codemirror,目前我在使用退格键来处理以前输入的文本时遇到了问题.这对我的用例来说是个大问题.现在我已经浏览了一下,看起来它不是直接的codemirror问题,而是android和虚拟键盘malarkey,请看这个问题:Android:WebView/BaseInputConnection中的Backspace

我正在使用Phonegap版本2.6.0,最新的codemirror版本(截至昨晚)和在Android 4.2.2上进行测试.这似乎是特定于Android上的WebView,任何人都可以验证这不是iOS上的问题吗?

我并不反对做一些Java代码来纠正这个问题,但我不确定如何"挂钩"到cordova的WebView实现中,因为所有暴露给我的代码都包括:

package com.mycompany.MyAppName;

import android.os.Bundle;
import org.apache.cordova.*;

public class MyAppName extends DroidGap{
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html")
    }
}
Run Code Online (Sandbox Code Playgroud)

除非我应该查看Cordovas源代码树.基本上我想知道的是如何在我的案例中在上面的链接中实现解决方案.任何帮助是极大的赞赏!

小智 10

覆盖init Activity方法:

public class ProjectName extends DroidGap
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        init(); // Don't forget this, you'll get runtime error otherwise!

        // The following does the trick:
        super.appView.getSettings().setUseWideViewPort(true);
        super.appView.getSettings().setLoadWithOverviewMode(true);

        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html")
        super.setIntegerProperty("loadUrlTimeoutValue", 10000); 
    }

    /**
     * Create and initialize web container with default web view objects.
     */
    @Override
    public void init() {
        CordovaWebView webView = new CustomWebView(ProjectName.this);
        CordovaWebViewClient webViewClient;
        if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
        {
            webViewClient = new CordovaWebViewClient(this, webView);
        }
        else
        {
            webViewClient = new IceCreamCordovaWebViewClient(this, webView);
        }
        this.init(webView, webViewClient, new CordovaChromeClient(this, webView));
    }

}
Run Code Online (Sandbox Code Playgroud)

在CustomWebView上创建,扩展了CordovaWebView

public class CustomWebView extends CordovaWebView{

    public CustomWebView(Context context) {
        super(context);
    }

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

        return connection;
    }

}
Run Code Online (Sandbox Code Playgroud)

创建自定义InputConnection:

public class MyCustomInputConnection extends BaseInputConnection{

    public MyCustomInputConnection(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);
    }
}
Run Code Online (Sandbox Code Playgroud)