【问题标题】:How to implement custom Gif keyboard in android?如何在Android中实现自定义GIF键盘?
【发布时间】:2017-12-01 08:12:04
【问题描述】:

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

【问题讨论】:

  • 您需要为此创建一个扩展 EditText 类的自定义类,请参阅我在@gowthami 下方的回答
  • 解决了吗? @gothami
  • 你能实现这个吗?我也必须这样做。请帮助我。
  • 有人有解决办法吗?

标签: android keyboard gif


【解决方案1】:

你需要像这样制作一个自定义的 EditText 类

 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

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 我是Android初学者。如果您有任何示例应用程序,请分享。我想创建相同类型的应用程序。非常感谢你。
    猜你喜欢
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 2016-10-23
    • 2017-04-07
    • 2014-09-27
    相关资源
    最近更新 更多