8

对于我的应用程序,我需要一个性能良好的基于​​ Web 的文本/代码编辑器。

我正在尝试在 Phonegap 下使用 codemirror,目前我在让退格键为以前输入的文本工作时遇到问题。这对我的用例来说是一个巨大的问题。现在我环顾四周,似乎这不是直接的代码镜像问题,而是 android 和虚拟键盘的问题,请参阅这个问题:Android: Backspace in WebView/BaseInputConnection

我正在使用 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")
    }
}

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

4

1 回答 1

10

覆盖初始化活动方法:

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));
    }

}

在扩展 CordovaWebView 的 CustomWebView 上创建

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;
    }

}

创建您的自定义 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);
    }
}
于 2013-05-29T19:35:44.853 回答