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

标签 android cordova input android-webview backspace

我的应用程序需要一个运行良好的基于​​网络的文本/代码编辑器。

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

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

我不反对做一些 Java 代码来纠正这个问题,但我不确定如何“ Hook ”到 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")
    }
}

除非我应该查看 Cordova 源代码树。基本上我想知道的是如何在我的案例中通过上面的链接实现解决方案。非常感谢任何帮助!

最佳答案

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

}

在扩展 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);
    }
}

关于android - 在 Android 4.x 的 Phonegap 下,无法让退格键在 codemirror 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16499178/

相关文章:

Android Recyclerview 在 scrolltop 上加载更多

javascript - Java Android - 从 WebView 获取图像

c - 在C中将字母数字存储为未知数量的数字

android - 当应用程序处于后台时,Firebase 推送通知不播放声音

android - SpannableGridView 改变布局问题

android - 从 HTML5 混合移动应用程序查看 PDF 文件

jquery - iOS HTML Web App (PhoneGap) 过度滚动

cordova - 如何将 www 更改为 Cordova 中的另一个目录?

没有 jQuery 的 JavaScript 日期时间选择器?

javascript - jQuery - 获取 &lt;input&gt; 抛出按键事件的索引