2013-05-11 77 views
8

我需要一個基於Web的文本/代碼編輯器,對我的應用程序來說表現很好。在Android 4.x下的Phonegap下,無法使用backspace在codemirror中工作?

我想在Phonegap下使用codemirror,目前我遇到的問題是使用退格鍵爲以前輸入的文本工作。這對我的用例來說是一個巨大的問題。現在我已經看了一下,它似乎不是一個直接的codemirror問題,而是android和虛擬鍵盤malarkey,看到這個問題: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源碼樹。基本上我想知道的是我如何在上面的鏈接中實現解決方案。任何幫助是極大的讚賞!

+0

是的,這不是一個特定的問題到CodeMirror,因爲它也出現在Ace中。當使用硬件鍵盤時,它也很棒。這是虛擬屏幕鍵盤的問題。 – Fred 2014-03-01 20:20:56

回答

9

覆蓋的init活動方法:

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

} 

創建於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; 
    } 

} 

創建自定義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); 
    } 
} 
+1

它的工作原理!非常感謝!我可以問一下這兩個自定義設置是用於「下面的技巧」評論之下的嗎?我不得不禁用它們,因爲頁面呈現出所有「最小化」,就好像比例不同。 – Nisk 2013-05-30 20:39:46

+0

我不remenber,我會禁用他們:p – max28 2013-05-31 11:37:09

+0

你讓我的一天!感謝十億! – 0m4r 2013-12-11 14:21:20

相關問題