在Android的EditText中从自定义键盘/Google键盘插入图像/贴纸/gif

原学程将引见在Android的EditText中从自界说键盘/Google键盘拔出图象/贴纸/gif的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

成绩描写

我试图应用相似Google键盘的键盘Gboard将emoji脸色拔出到我的edittext中,但是显示为toastThis text field does not support GIF insertion from the keyboard

闭于这个成绩有多少个成绩,但是出有准确的谜底。我浏览了documentation reference given,但是未给出完成。我测验考试了此操纵,但是未触收onCo妹妹itContent

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

  final InputConnectionCompat.OnCo妹妹itContentListener callback =
new InputConnectionCompat.OnCo妹妹itContentListener() {
 @Override
 public boolean onCo妹妹itContent(InputContentInfoCompat inputContentInfo,
int flags, Bundle opts) {
  // read and display inputContentInfo asynchronously
  if (BuildCompat.isAtLeastNMR一() && (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等运用法式支撑这1功效。我能否须要创立自界说EditText或者其余实质?

推举谜底

如您的成绩所示,您仿佛出有树立实质MIME典型。我曾经创立了1个戴有回调keyBoardInputCallbackListenerEditText,它检测能否经由过程硬键盘拔出了所有gif/png/jpg/webp

public class MyEditText extends android.support.v七.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.OnCo妹妹itContentListener callback =
  new InputConnectionCompat.OnCo妹妹itContentListener() {
@Override
public boolean onCo妹妹itContent(InputContentInfoCompat inputContentInfo,
 int flags, Bundle opts) {

 // read and display inputContentInfo asynchronously
 if (BuildCompat.isAtLeastNMR一() && (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.onCo妹妹itContent(inputContentInfo, flags, opts);
 }
 return true;  // return true if succeeded
}
  };

  public interface KeyBoardInputCallbackListener {
void onCo妹妹itContent(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;
  }
}

在您的运动课中应用此选项-

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

佳了闭于在Android的EditText中从自界说键盘/Google键盘拔出图象/贴纸/gif的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。

0
没有账号?注册  忘记密码?