如何在 android 中实现自定义 Gif 键盘?

How to implement custom Gif keyboard in android?

我想在自定义上使用 gif 图像 keyboard.can 谁能帮我如何实现自定义 Gif 键盘?

您可以按照本教程创建自定义键盘: https://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615

然后在实现您自己的图像或 gif 时,通过添加扩展弹出窗口 window 的 class 创建一个包含您的图像的自定义弹出窗口 window。看到这个答案:

implement popup window on keyboard in android to add images in custom keyboard

在您的键盘中添加一个具有唯一键码的键,该键将触发并显示弹出窗口 window。

您需要像这样制作自定义 EditText Class

 public class GifEditText extends EditText {
        public GifEditText(Context context) {
            super(context);
        }

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

        public GifEditText(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        @Override
        public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
            final InputConnection ic = super.onCreateInputConnection(editorInfo);
            EditorInfoCompat.setContentMimeTypes(editorInfo,
                    new String[]{"image/gif"});

            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);
        }
    }

然后像这样使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.test.GifEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Test gif" />
</LinearLayout>

更多细节可以参考官方文档: https://developer.android.com/guide/topics/text/image-keyboard.html

我通过实现这个获得了示例,我们可以简单地创建自定义 gif 键盘

https://github.com/googlesamples/android-CommitContentSampleIME