在Android的EditText中从自定义键盘/Google键盘插入图像/贴纸/gif [英] Insert images/sticker/gif in Edittext in android from custom keyboard/google keyboard

查看:19
本文介绍了在Android的EditText中从自定义键盘/Google键盘插入图像/贴纸/gif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用类似Google键盘的键盘Gboard将emoji表情插入到我的edittext中,但显示为toastThis 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或其他内容?

推荐答案

如您的问题所示,您似乎没有设置内容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
                        }
                    });

这篇关于在Android的EditText中从自定义键盘/Google键盘插入图像/贴纸/gif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆