如何在最新 Android 7.1 Nougat 中实现图像和 Gif 键盘
How do I implement Image & Gif Keyboard in Latest Android 7.1 Nougat
我需要在 Android 7.1 Nougat 中实现最新的图像和 Gif 键盘,请参见下面的屏幕截图。你能解释一下我怎样才能做到这一点。如果用例子解释更明显。请参阅此 Android 文档 Image Keyboard Support
像这样制作自定义 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>
我需要在 Android 7.1 Nougat 中实现最新的图像和 Gif 键盘,请参见下面的屏幕截图。你能解释一下我怎样才能做到这一点。如果用例子解释更明显。请参阅此 Android 文档 Image Keyboard Support
像这样制作自定义 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>