gpt4 book ai didi

android - 从自定义键盘/谷歌键盘在android中的Edittext中插入图像/贴纸/gif

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:12 27 4
gpt4 key购买 nike

我尝试使用 Google 键盘 Gboard 等键盘将表情符号插入到我的 edittext 但它显示 toast 此文本字段不支持从键盘插入 GIF.

enter image description here

有几个关于相同的问题,但没有正确的答案。我读了documentation reference given ,但没有给出实现。我试过了,但它没有触发 onCommitContent -

EditText editText = new EditText(this) {
@Override
public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
final InputConnection ic = super.onCreateInputConnection(editorInfo);

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

但 Whatsapp、Telegram 等应用程序支持此功能。我是否需要创建自定义 EditText 或其他内容?

最佳答案

正如您在问题中给出的那样,您似乎没有设置内容 mime 类型。我创建了一个带有回调 keyBoardInputCallbackListenerEditText,它检测是否通过软键盘插入了任何 gif/png/jpg/webp

public class MyEditText extends android.support.v7.widget.AppCompatEditText {

private String[] imgTypeString;
private KeyBoardInputCallbackListener keyBoardInputCallbackListener;

public MyEditText(Context context) {
super(context);
initView();
}

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

private void initView() {
imgTypeString = new String[]{"image/png",
"image/gif",
"image/jpeg",
"image/webp"};
}

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
final InputConnection ic = super.onCreateInputConnection(outAttrs);
EditorInfoCompat.setContentMimeTypes(outAttrs,
imgTypeString);
return InputConnectionCompat.createWrapper(ic, outAttrs, callback);
}


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
}
}
boolean supported = false;
for (final String mimeType : imgTypeString) {
if (inputContentInfo.getDescription().hasMimeType(mimeType)) {
supported = true;
break;
}
}
if (!supported) {
return false;
}

if (keyBoardInputCallbackListener != null) {
keyBoardInputCallbackListener.onCommitContent(inputContentInfo, flags, opts);
}
return true; // return true if succeeded
}
};

public interface KeyBoardInputCallbackListener {
void onCommitContent(InputContentInfoCompat inputContentInfo,
int flags, Bundle opts);
}

public void setKeyBoardInputCallbackListener(KeyBoardInputCallbackListener keyBoardInputCallbackListener) {
this.keyBoardInputCallbackListener = keyBoardInputCallbackListener;
}

public String[] getImgTypeString() {
return imgTypeString;
}

public void setImgTypeString(String[] imgTypeString) {
this.imgTypeString = imgTypeString;
}
}

在你的 Activity 类中使用这个 -

MyEditText myEditText = (MyEditText)findViewbyId(R.id.myEditText);
myEditText.setKeyBoardInputCallbackListener(new KeyBoardInputCallbackListener() {
@Override
public void onCommitContent(InputContentInfoCompat inputContentInfo,
int flags, Bundle opts) {
//you will get your gif/png/jpg here in opts bundle
}
});

关于android - 从自定义键盘/谷歌键盘在android中的Edittext中插入图像/贴纸/gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50057942/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com