Необходимо переопределить функцию [onCreateInputConnection] для TextView - PullRequest
0 голосов
/ 10 марта 2020

Я немного поболтал и хочу использовать свою клавиатуру Google (GBoard) для отправки картинок. Когда я нажал на Gif, у меня появилось это сообщение: «Приложение не поддерживает вставку изображения здесь».

Я посмотрел онлайн и увидел, что мне нужно переопределить функцию onCreateInputConnection () для TextView, как сказано в этом ссылка: https://developer.android.com/guide/topics/text/image-keyboard#java

Если я правильно понимаю, EditText является нативным компонентом TextView. Но я действительно не знаю, как я могу это переопределить.

Я транспонировал код java в машинописном тексте, но не могу проверить, и я не знаю, как его проверить прямо сейчас.

export class CustomTextView extends android.widget.TextView {    

    public onCreateInputConnection(editorInfo: android.view.inputmethod.EditorInfo): android.view.inputmethod.InputConnection {
        var inputConnection = super.onCreateInputConnection(editorInfo);
        androidx.core.view.inputmethod.EditorInfoCompat.setContentMimeTypes(editorInfo, Array("image/gif", "image/png"));

        var callback = new androidx.core.view.inputmethod.InputConnectionCompat.OnCommitContentListener();
        callback.onCommitContent = (inputContentInfo: androidx.core.view.inputmethod.InputContentInfoCompat, flags: number, opts: globalAndroid.os.Bundle): boolean => {
            if (androidx.core.os.BuildCompat.isAtLeastNMR1() && (flags &
                androidx.core.view.inputmethod.InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
                try {
                    inputContentInfo.requestPermission();
                }
                catch (e) {
                    return false;
                }
            }
            return true;
        }
        return androidx.core.view.inputmethod.InputConnectionCompat.createWrapper(inputConnection, editorInfo, callback);
    }
}

Я не нашел никакой помощи в Интернете ...

Спасибо!

1 Ответ

0 голосов
/ 11 марта 2020

Большое спасибо за ваш ответ @manoj. Поэтому я попытался создать компонент, как вы сказали, и переопределить onCreateInputConnection.

Это вполне работает. У меня больше нет этого сообщения: «Приложение не поддерживает вставку изображения здесь», но дальше ничего не происходит, и я не знаю, что мне нужно делать ...

Вот мой код:

import { CustomTextEditBase } from "./custom-text-edit.common";

class CustomAndroidWidgetEditText extends android.widget.EditText {

    constructor(context: globalAndroid.content.Context) {
        super(context);
    }

    public onCreateInputConnection(editorInfo: android.view.inputmethod.EditorInfo): android.view.inputmethod.InputConnection {
        var inputConnection = super.onCreateInputConnection(editorInfo);
        let mimeTypesArray : native.Array<string> = ["image/gif", "image/png", "image/jpg"];
        androidx.core.view.inputmethod.EditorInfoCompat.setContentMimeTypes(editorInfo, mimeTypesArray);

        var callback = new androidx.core.view.inputmethod.InputConnectionCompat.OnCommitContentListener({
            onCommitContent: (inputContentInfo: androidx.core.view.inputmethod.InputContentInfoCompat, flags: number, opts: globalAndroid.os.Bundle): boolean => {
                if (androidx.core.os.BuildCompat.isAtLeastNMR1() && (flags &
                    androidx.core.view.inputmethod.InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
                    try {
                        inputContentInfo.requestPermission();
                    }
                    catch (e) {
                        return false;
                    }
                }

                var gifSupported: boolean = false;
                for (let mimeType in mimeTypesArray) {
                    if (inputContentInfo.getDescription().hasMimeType(mimeTypesArray[mimeType])) {
                        gifSupported = true;
                    }
                }
                if(!gifSupported) return false;

                androidx.core.view.inputmethod.InputConnectionCompat.commitContent(inputConnection, editorInfo, inputContentInfo, flags, opts);
                return true;
            }
        });
        return androidx.core.view.inputmethod.InputConnectionCompat.createWrapper(inputConnection, editorInfo, callback);
    }
}

export class CustomTextEdit extends CustomTextEditBase {

    nativeView: CustomAndroidWidgetEditText;

    public createNativeView(): Object {
        const editText = new CustomAndroidWidgetEditText(this._context);
        return editText;
    }

    initNativeView(): void {
        (<any>this.nativeView).owner = this;
        super.initNativeView();
    }

    disposeNativeView(): void {
        (<any>this.nativeView).owner = null;
        super.disposeNativeView();
    }
}
...