4

我需要在 Android 7.1 Nougat 中实现最新的图像和 Gif 键盘,请参见下面的屏幕截图。你能解释一下我怎么能做到这一点。如果用例子解释更明显。请参阅此 Android 文档图像键盘支持 在此处输入图像描述

4

1 回答 1

5

像这样制作自定义 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>
于 2017-01-26T08:32:40.713 回答