【问题标题】:Android: Backspace in WebView/BaseInputConnectionAndroid:WebView/BaseInputConnection 中的退格键
【发布时间】:2013-01-11 16:36:02
【问题描述】:

我在 Android (4.2) 中遇到了软键盘退格问题。

我在 WebView (CodeMirror) 中有一个自定义编辑器,它在内部使用一个空的 <textarea>。 Android 系统似乎不会发送退格,除非它认为<textarea> 中有一些文本。

我已覆盖 WebView onCreateInputConnection 以尝试降低软输入:

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    Log.d("CustomWebView", "onCreateInputConnection(...)");
    BaseInputConnection connection = new BaseInputConnection(this, false);
    outAttrs.inputType = InputType.TYPE_NULL;
    outAttrs.imeOptions = EditorInfo.IME_ACTION_NONE;
    outAttrs.initialSelStart = -1;
    outAttrs.initialSelEnd = -1;

    return connection;
}

但是,这不起作用,甚至onKeyUp 也不需要退格。

如何强制软键盘始终发送退格键?

【问题讨论】:

    标签: android android-input-method android-4.2-jelly-bean


    【解决方案1】:

    好的,终于明白了。

    在 Android 4.2(可能还有早期版本)中,退格键不会通过标准软键盘作为 sendKeyEvent(..., KeyEvent.KEYCODE_DEL) 发送。相反,它以deleteSurroundingText(1, 0) 发送。

    因此,在我的情况下,解决方案是使用以下内容制作自定义 InputConnection

    @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 编写的第三天。

    【讨论】:

    • 嘿,我也遇到了这个问题...除了我使用的是Phonegap,所以我对java源代码没有太多的低级访问权限,因为它都是编译的.我只能访问 onCreate 方法,其他所有内容都被混淆/编译。有什么建议吗?
    • 抱歉,我从来没有使用过PhoneGap,所以我无法回答您的问题——但请尝试将其作为一个单独的问题提出,应该有熟悉的人。
    • 无论如何,谢谢,我已经发布了这个问题,如果你想关注它:stackoverflow.com/questions/16499178/…
    • 不错的发现。这让我发疯了。
    • 非常好的发现。谢谢老兄。
    【解决方案2】:

    这段代码会更好,它适用于更多键盘:D

    @Override
      public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
          outAttrs.actionLabel = null;
          outAttrs.inputType = InputType.TYPE_NULL;
          final InputConnection con = new BaseInputConnection(this,false);
          InputConnectionWrapper 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
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多