15

Android(4.2)のソフトキーボードのバックスペースに問題があります。

<textarea>WebView(CodeMirror)に、内部で空を使用するカスタムエディターがあります。にテキストが含まれていると思われない限り、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バックスペースに対しても呼び出されません。

ソフトキーボードに常にバックスペースを送信させるにはどうすればよいですか?

4

2 に答える 2

34

さて、ついにこれを理解しました。

sendKeyEvent(..., KeyEvent.KEYCODE_DEL)Android 4.2(以前のバージョンでも)では、バックスペースは標準のソフトキーボードでは送信されません。代わりに、として送信され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向けの執筆は3日目なので、ここで愚かなことをしている場合はお知らせください。

于 2013-01-28T11:47:01.690 に答える
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) ;
      }
  }
于 2016-10-27T06:09:23.437 に答える