从自定义键盘/谷歌键盘在 android 中的 Edittext 中插入图像/贴纸/gif

2024-01-13

我尝试使用 Google 键盘等键盘插入表情符号Gboard to my edittext但它显示吐司This text field does not support GIF insertion from the keyboard.

有几个关于相同问题但没有正确答案的问题。我读了给出的文档参考 https://developer.android.com/guide/topics/text/image-keyboard#how_it_works,但没有给出实现。我尝试了这个,但它没有触发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 类型。我创建了一个EditText带回调keyBoardInputCallbackListener检测是否有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 inputContentInfo 
                       // You can use a webView or ImageView to load the gif

                            Uri linkUri = inputContentInfo.getLinkUri();
                            mWebView.loadUrl(linkUri != null ? linkUri.toString() : "null");

                    }
                    });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从自定义键盘/谷歌键盘在 android 中的 Edittext 中插入图像/贴纸/gif 的相关文章

随机推荐

  • 对象引用和对象哈希码之间的区别

    java中对象的引用和同一对象的哈希码值有什么区别 它们是完全不同的两个概念 Cat oldCat new Cat Cat newCat new Cat Cat oldCatRef oldCat 在上面的例子中 oldCat and old
  • 在 SQL 输出中插入双引号

    例如 在运行查询并查看输出后 select from People 我的输出如下 First Last Email Ray Smith email protected cdn cgi l email protection 我如何导出这些数据
  • 如何使用 JavaScript 检查文件是否存在?

    如何使用 JavaScript 检查文件是否存在 在本例中我想检查的是 xml 文件 如果您使用的是 jQuery 您可以尝试加载该文件 ajax type GET url some xml success function found e
  • 如何强制 pytest 写入颜色输出?

    即使在写入管道时 如何强制 pytest 以颜色显示结果 似乎没有任何命令行选项可以执行此操作 从 2 5 0 开始 py test 有这个选项 color yes 从 2 7 0 开始 还应该可以执行以下操作 export PYTEST
  • Spring 与 JSF 2 的比较 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 如何两两比较多次测量的增减?

    我有一个数据 我想通过取差值来两两比较测量值 如果差值大于 0 2 则添加另一列为 是 否则为 否 就像图像中的示例数据一样 并且 在每个人的最后 如果至少有一个 是 则最终结果为 是 我已手动填写第一行 但我想对我的所有个人 1000 个
  • Python 并删除列表列表中的重复项,无论列表中的顺序如何

    我已经搜索过 但没有找到与我相同的问题 我想从 python 中的列表列表中删除重复项 但是 我不在乎列表中值的顺序 我目前的做法太耗时了 我想做的事 A 1 2 3 2 3 4 3 4 5 3 2 4 我想搜索 A 并删除所有重复项 这里
  • 使用python同时向多个CC和多个TO收件人发送电子邮件

    分别尝试了多个 to 和多个 cc 效果很好 但是当我尝试两者时 出现错误 File 路径 Continuum anaconda2 envs mypython lib smtplib py 第 870 行 在 sendmail sender
  • std::string 用 bool 初始化

    考虑以下初始化 std string falseString false std string trueString true With g 5 2 0 编译器会抛出警告falseString 而错误为trueString With cla
  • 需要使用 R 从字符串列中提取单个字符

    背景 下面是我的gamedatadput 形式的数据集 它包含一些 MLB 比赛的比分 structure list team c NYM NYM BOS NYM BOS linescore c 010000000 10 1140006x
  • 如何在 Dhall 中将动态命名记录与静态记录合并?

    我正在 Dhall 中创建 AWS Step Function 定义 但是 我不知道如何创建他们使用的通用结构Choice如下例所示 Not Variable type StringEquals Private Next Public Th
  • 如何保存并添加游戏中获得的数值?

    我正在为我的游戏制作一个商店 我正在制作一个系统 我在游戏中获得的分数被保存 每次玩时它也会添加和保存 但这最后一部分不起作用我不知道为什么 我从另一个名为的脚本中获取一个变量score它收到我得到的值 并且必须将其添加到以前的值 但它不会
  • 如何计算销售范围内的排名

    如何计算销售级别定义的类别内的排名 假设我们希望将销售额高于某个阈值的产品标记为 高 类别 将低于该阈值的产品标记为 低 类别 这是一个示例数据 let Source Table FromRows Json Document Binary
  • 将 Vuetify 工具提示 (v-tooltip) 组件与外部激活器(即未包装)一起使用

    我了解如何使用 Vuetifyv 工具提示 https vuetifyjs com en components tooltips introduction工具提示包裹着组件 但是 我不太确定如何将激活按钮放在外面 例如我有这个 非工作代码
  • request.getServletPath() 从 Spring MVC 返回 null

    我做了一个过滤器来捕获HttpServletRequest所有请求的 servlet 路径 Override public void doFilter ServletRequest req ServletResponse res Filte
  • react-native-multiple-select 存储提交时选择的项目

    我正在使用react native multiple select并尝试创建一个下拉菜单 允许用户选择多个选项 然后将他们选择的选项记录到数组中 目前 我的代码是 onSelectedItemsChange selectedItems gt
  • 使用javascript检测设备CPU/GPU性能?

    这个问题并不特定于 Three js 但我会用它作为例子 我最近一直在使用 Three js 开发 Web 应用程序界面 并在 WebGL 和 Canvas 渲染器 针对桌面浏览器 之间编写了一些不错的后备程序 但现在的问题变成了如何正确检
  • C/C++ 中 \x 是什么意思?

    Example char arr xeb x2a BTW 以下内容相同吗 xeb x2a vs xeb x2a x表示十六进制字符转义 它用于指定不可输入的字符 例如 null x00 And xeb x2a 是一个文字字符串 类型是cha
  • git pull --rebase:传递 --rebase-merges

    这是我在重新调整当前分支的基础时通常会做的事情 同时防止本地分支变平 git fetch origin git rebase r origin develop r is rebase merges 我更喜欢 preserve merges
  • 从自定义键盘/谷歌键盘在 android 中的 Edittext 中插入图像/贴纸/gif

    我尝试使用 Google 键盘等键盘插入表情符号Gboard to my edittext但它显示吐司This text field does not support GIF insertion from the keyboard 有几个关