【问题标题】:Insert images/sticker/gif in Edittext in android from custom keyboard/google keyboard从自定义键盘/谷歌键盘在 android 中的 Edittext 中插入图像/贴纸/gif
【发布时间】:2018-04-27 08:01:43
【问题描述】:

我尝试使用像 Google 键盘 Gboard 这样的键盘将表情符号插入到我的 edittext,但它显示 toast This text field does not support GIF insertion from the keyboard

有几个相同的问题,但没有正确的答案,。我读了documentation reference given,但没有给出实现。我试过了,但它没有触发onCommitContent -

EditText editText = new EditText(this) {
    @Override
    public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
        final InputConnection ic = super.onCreateInputConnection(editorInfo);

        final InputConnectionCompat.OnCommitContentListener callback =
            new InputConnectionCompat.OnCommitContentListener() {
                @Override
                public boolean onCommitContent(InputContentInfoCompat inputContentInfo,
                        int flags, Bundle opts) {
                    // read and display inputContentInfo asynchronously
                    if (BuildCompat.isAtLeastNMR1() && (flags &
                        InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
                        try {
                            inputContentInfo.requestPermission();
                        }
                        catch (Exception e) {
                            return false; // return false if failed
                        }
                    }

                    // read and display inputContentInfo asynchronously.
                    // call inputContentInfo.releasePermission() as needed.

                    return true;  // return true if succeeded
                }
            };
        return InputConnectionCompat.createWrapper(ic, editorInfo, callback);
    }
};

但 Whatsapp、Telegram 等应用程序支持此功能。我需要创建自定义 EditText 吗?

【问题讨论】:

  • 我认为你错过了setmimetype

标签: android android-edittext android-softkeyboard


【解决方案1】:

根据您的问题,您似乎没有设置内容 mime 类型。我创建了一个带有回调keyBoardInputCallbackListenerEditText,它检测是否通过软键盘插入了任何gif/png/jpg/webp

public class MyEditText extends android.support.v7.widget.AppCompatEditText {

  private String[] imgTypeString;
  private KeyBoardInputCallbackListener keyBoardInputCallbackListener;

  public MyEditText(Context context) {
      super(context);
      initView();
  }

  public MyEditText(Context context, AttributeSet attrs) {
      super(context, attrs);
      initView();
  }

  private void initView() {
      imgTypeString = new String[]{"image/png",
            "image/gif",
            "image/jpeg",
            "image/webp"};
  }

  @Override
  public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
      final InputConnection ic = super.onCreateInputConnection(outAttrs);
      EditorInfoCompat.setContentMimeTypes(outAttrs,
              imgTypeString);
      return InputConnectionCompat.createWrapper(ic, outAttrs, callback);
  }


  final InputConnectionCompat.OnCommitContentListener callback =
        new InputConnectionCompat.OnCommitContentListener() {
            @Override
            public boolean onCommitContent(InputContentInfoCompat inputContentInfo,
                                           int flags, Bundle opts) {

                // read and display inputContentInfo asynchronously
                if (BuildCompat.isAtLeastNMR1() && (flags &
                        InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
                    try {
                        inputContentInfo.requestPermission();
                    } catch (Exception e) {
                        return false; // return false if failed
                    }
                }
                boolean supported = false;
                for (final String mimeType : imgTypeString) {
                    if (inputContentInfo.getDescription().hasMimeType(mimeType)) {
                        supported = true;
                        break;
                    }
                }
                if (!supported) {
                    return false;
                }

                if (keyBoardInputCallbackListener != null) {
                    keyBoardInputCallbackListener.onCommitContent(inputContentInfo, flags, opts);
                }
                return true;  // return true if succeeded
            }
        };

  public interface KeyBoardInputCallbackListener {
      void onCommitContent(InputContentInfoCompat inputContentInfo,
                         int flags, Bundle opts);
  }

  public void setKeyBoardInputCallbackListener(KeyBoardInputCallbackListener keyBoardInputCallbackListener) {
      this.keyBoardInputCallbackListener = keyBoardInputCallbackListener;
  }

  public String[] getImgTypeString() {
      return imgTypeString;
  }

  public void setImgTypeString(String[] imgTypeString) {
      this.imgTypeString = imgTypeString;
  }
}

在您的活动课程中使用它 -

MyEditText myEditText = (MyEditText)findViewbyId(R.id.myEditText);
myEditText.setKeyBoardInputCallbackListener(new KeyBoardInputCallbackListener() {
                        @Override
                        public void onCommitContent(InputContentInfoCompat inputContentInfo,
                             int flags, Bundle opts) {
                        //you will get your gif/png/jpg here in opts bundle
                        }
                    });

【讨论】:

  • 谢谢 karan.. 也许你可以在那之后添加关于如何处理 gif 链接的部分..
  • 我在我的应用程序中使用了这个代码。它工作正常,但在捆绑中遇到一个问题我收到空值。你能帮我解决这个问题吗?
  • 做了和上面一样,但是在我当前的活动中不能覆盖 onCommitContent()
猜你喜欢
  • 1970-01-01
  • 2017-07-26
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 2011-02-17
  • 2017-12-01
  • 2018-09-21
  • 2017-07-05
相关资源
最近更新 更多