Android implements rich text editing of EditText

preface

This article is an upgraded version of the article I wrote earlier, "Android mixed text layout - realizing EditText mixed text insertion and upload". In addition to realizing image upload in EditText, it also includes video upload, cloud disk file upload, recording upload and displaying upload progress. It is currently applied to bee assembly number task module.

First, introduce the implementation effect of this function:

Realization idea

The implementation idea is slightly different from that described earlier, but it is still implemented using spannablestring. Since it supports not only image upload, but also audio, video and file upload, in order to facilitate the expansion of more types in the future, the tag implementation is no longer used here, but directly implemented in JSON. The previous implementation idea was "< img url =" XXX. JPG ">", now each rich text element is replaced by strings such as "{" type ":" video "," data ": {" URL ":" XXX. MP4 "," thumb ":" Base64 STR "," size ": 1024}}". The "type" has types such as "video", "audio", "image", "text" and "file". For different types, the fields in "data" are also different. "Data" generally contains fields such as file name, file size, file network path, audio and video length, etc.

When the upload progress of pictures or videos changes, switch back to the main thread to constantly update the UI. The so-called UI update is actually to constantly replace the spannablestring. For various styles of imagespan, it is actually bitmapdrawable.

Implements the insertion of rich text elements into EditText

The implementation code is as follows:

The taskspan here inherits the imagespan and converts the view of the audio playback bar into drawable, so it can be displayed in EditText. Similarly, the implementation of pictures, videos and files is the same.

Implement the click event of rich text element

To click video to jump to the video playing page, click audio to play audio, and click file to jump to the file preview page, click events must be added to these rich text elements. The common implementation here is the custom linkmovementmethod:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>