Use multiple layouts, each corresponding to an HTML format, such as pictures, such as ordered lists, etc. Specific implementation examples can refer to this link. Medium sum
This is how Evernote's rich text editing is realized. Generally speaking, it is more complicated.
WebView+JavaScript implementation. Now there are many mature JavaScript rich text editing libraries on the Web, such as Squire. You just need to do it well.
The interaction between WebView and JavaScript is enough (write more callback functions). Although this is true in theory, you need to solve the compatibility problem of WebView in the implementation process (
Android 4.4 and above are different from WebView kernel below 4.4), and other unforeseen problems (such as the problem of not being able to paste text).
TextView for editing text+Span .Android natively supports bold, strikethrough, reference and so on.
, to achieve simple rich text editing needs, operability is still relatively large. After comprehensive consideration, I chose this way to realize my needs.
Since we have decided to implement it with EditText+Span, we should know something about the related API.
First, let's learn about Span. Span is a very powerful concept, and students who are interested in further study recommend reading this translation directly.
Two main types of spans are used here:
Span inherited from CharacterStyle, such as StyleSpan, can be bold and underlined at the character level.
Span inherited from ParagraphStyle, such as QuoteSpan, can add references to paragraph-level text.
Then we need a text structure that can set the effect of Span into it (that is, the Spannable interface is implemented), SpannableStringBuilder.
It is a good choice, and the getEditableText () method provided by EditText is also available. Usually you only need getEditableText ()
Just do it, but in the face of some details, you can preset the corresponding span with SpannableStringBuilder and then replace it with the original text.
The way to set Span is also very simple. You need to call span. setspan (object what,int start,int end,int。
Flags) This method is enough, and the four parameters in the method are explained as follows:
What object? Pass in the Span object you used.
Int start, set the starting position of the span.
Int end, set the end position of the span.
Int flags, indicating the range of setting Span.
Here, I will focus on the parameter int flag, which accepts four types of parameters, namely:
Leap. Span _ inclusive _ exclusive means to enter text before setting the Span area, and the entered text will also be affected by Span.
The influence of.
Leap. Span _ inclusive _ inclusive refers to entering text before and after the area where Span is set, and all the entered text is subject to Span.
The influence of.
Leap. Span _ exclusive _ exclusive, which means that only when you type the input text in the area where Span is set, the input text will obey Span.
The influence of.
Leap. Span _ exclusive _ inclusive refers to the text input after setting the Span area, and the input text will also be affected by Span.
The influence of.
"Affected" means that you will still keep the style of the Span you set, such as choosing spanned. span _ exclusive _ inclusive。
If a paragraph of text is set to bold, the newly entered text after the paragraph will also be bold. Span _ exclusive _ exclusive is recommended here.
Parameters, after all, other parameters are relatively difficult to control, which will bring confusion to users. It is believed that the behavior of operation representatives should be accurate.
Well, here we already know how to make a rich text editor component, which is nothing more than specifying the starting position and ending position, and then setting the corresponding span.
Do it. As for the rules used in setting, you can customize them yourself. But it only solves the problem of editing, as well as the problem of importing and exporting.
The problem of import is simple. The method of Html.fromHtml () is provided in the Android SDK, which can easily convert the HTML string into the required string.
Cross-regional objects. However, it should be noted that Html.fromHtml () does not support all HTML tags, such as unordered lists, so it needs to be implemented by itself.
Html。 The TagHandler interface handles the tags you need. You can refer to this link to support strikethrough and simple unordered lists.
Facing character-level styles such as bold and italic, Html.fromHtml ()
Naturally, it will be solved. Add line breaks where it should be added, no problem; However, in the face of paragraph-level styles such as references and unordered lists, this method will add a line break, that is, two line breaks, which is equivalent to an extra blank line. Generally speaking, people think that a person
Corresponding to two
But if you have special needs, you can also analyze them yourself as mentioned above, instead of using the default system.
I have introduced how to import before. You must be very clear that there must be a corresponding Html.toHtml () method! Yes, but unfortunately, this method does not support all spans.
For example, lists are not supported. But it doesn't matter, the source code of Html.toHtml () itself is easy to understand and can be used for reference.
This paper focuses on an interface method nextspantransfontrol(int start, int limit, class) of Spannanle.
Type), this method will return the starting position of the next span type you specify within the specified text range. In this way, the specified span can be scanned layer by layer.
It is very useful not to consider the influence of other types of spans at the same time.
Finally, despite this, there is still a key problem in importing and exporting, that is, the imported content should be consistent with the exported content. It is difficult for me to do this at present. I can only say that it should be controlled as much as possible, and regularization is needed to deal with the imported and exported text if necessary.