【问题标题】:Webview don't show letter from input on Android?Webview 不显示来自 Android 输入的字母?
【发布时间】:2015-05-26 09:49:19
【问题描述】:

我对@9​​87654321@ 有疑问,我使用zsseditor 和WebView 来显示、从键盘输入文本、插入图像或视频。然后,我无法删除段落中的最新字母(几乎我无法删除图像 html 标记)。我通过 CustomWebview 解决了这个问题,删除了图像 html 标签。

public class CustomWebView extends WebView {


/**
 * Constructs a new WebView with a Context object.
 *
 * @param context a Context object used to access application assets
 */
public CustomWebView(Context context) {
    super(context);
}

/**
 * Constructs a new WebView with layout parameters.
 *
 * @param context a Context object used to access application assets
 * @param attrs   an AttributeSet passed to our parent
 */
public CustomWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

/**
 * Constructs a new WebView with layout parameters and a default style.
 *
 * @param context  a Context object used to access application assets
 * @param attrs    an AttributeSet passed to our parent
 * @param defStyle the default style resource ID
 */
public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

// http://stackoverflow.com/questions/14560344/android-backspace-in-webview-baseinputconnection
// http://stackoverflow.com/questions/164991Image78/cant-get-backspace-to-work-in-codemirror-under-phonegap-on-android-4-x
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    return new MyCustomInputConnection(this, false);
}

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

我将输入键盘换成日语、越南语、泰语或除字母表以外的所有语言。但是当我点击键盘上的按钮字母时,它不会显示在WebView 上。那么如何修复WebView 可以显示每种语言的字母呢?

【问题讨论】:

  • 我用其他方法解决了,没有使用img标签

标签: android webview


【解决方案1】:

我和你有同样的问题,用这段代码解决 你只是在你的 webview 中覆盖

```

@Override
  public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
      outAttrs.actionLabel = null;
      outAttrs.inputType = InputType.TYPE_NULL;
      final InputConnection con = new BaseInputConnection(this,false);
      public_con = new InputConnectionWrapper(
              super.onCreateInputConnection(outAttrs), true) {
          @Override
          public boolean deleteSurroundingText(int beforeLength, int afterLength) {
              if (beforeLength == 1 && afterLength == 0) {
                  return this.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
                          && this.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
              }
              return super.deleteSurroundingText(beforeLength, afterLength);
          }

          @Override
          public boolean sendKeyEvent(KeyEvent event) {
              if(event.getKeyCode() == KeyEvent.KEYCODE_DEL){
                  return con.sendKeyEvent(event);
              }else {
                  return super.sendKeyEvent(event);
              }
          }
      };
      try {
          return public_con ;
      }catch (Exception e){
          return super.onCreateInputConnection(outAttrs) ;
      }
  }

```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多