我如何实现Image&最新Android 7.1牛轧糖中的Gif键盘

时间:2016-11-23 12:05:35

标签: android image keyboard gif android-7.1-nougat

我需要实施最新图像& Android 7.1 Nougat中的Gif键盘,见下面的屏幕截图。请问任何人请解释我怎样才能实现这一目标。如果用例子解释更明显。查看此Android文档Image Keyboard Support enter image description here

1 个答案:

答案 0 :(得分:4)

制作一个像这样的自定义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>