简体   繁体   English

在Android中实现富文本编辑器?

[英]Implementing a rich text editor in Android?

I am wondering if there are any good options to implement a rich text editor in Android. 我想知道是否有任何好的选择可以在Android中实现富文本编辑器 Please note I am talking about a rich text editor that can be used in an Android application, not the one embedded in a web page using HTML and Javascript. 请注意,我所说的是一种可以在Android应用程序中使用的富文本编辑器,而不是使用HTML和Javascript嵌入在网页中的富文本编辑器。

My requirements are: 我的要求是:

  • Basic formatting (color, fonts, highlight, bold, italic, underline, etc.) 基本格式(颜色,字体,突出显示,粗体,斜体,下划线等)
  • Hyperlinks 超连结
  • Inline images 内联图像
  • Bullet lists and numbered lists 项目符号列表和编号列表
  • Inline table (only the contents inside a cell is editable, not the table structure) 内联表(仅单元格内的内容可编辑,表结构不可编辑)

As you can see, this is pretty much something quite similar to a typical RichEdit control on Windows. 如您所见,这几乎类似于Windows上的典型RichEdit控件。

Here are some efforts (investigation & prototyping) I have made so far: 到目前为止,我已经做了一些努力(调查和原型设计):

Using WebView 使用WebView

I have tried using a WebView control to load an HTML fragment with one . 我尝试使用WebView控件加载带有HTML片段的HTML片段。 The content becomes editable and as it is HTML, I suppose it can meet most of my requirements. 内容变成可编辑的,因为它是HTML,所以我想它可以满足我的大多数要求。 But it has several issues: 但这有几个问题:

  • (deadly) No text caret. (致命)没有文字插入符号。 The user will have no idea where his/her typed characters will be inserted. 用户将不知道他/她键入的字符将插入哪里。
  • The on-screen soft keyboard is not visible by default. 默认情况下,屏幕上的软键盘不可见。 There is a trick that the user has to long-press the Menu button to bring up the keyboard. 用户必须长按菜单按钮才能调出键盘,这是一个技巧。 But I think this is a very bad user experience. 但是我认为这是非常糟糕的用户体验。 Besides, the screen layout is not properly rearranged and the text inserting point sometimes will be covered by the keyboard. 此外,屏幕布局没有正确地重新布置,并且文本插入点有时会被键盘覆盖。

Using EditText 使用EditText

I have tried using the EditText control. 我尝试使用EditText控件。 It seems to support some level of rich text editing (color, fonts, bold, italic, underline, inline images, bullet lists). 它似乎支持某种程度的富文本编辑(颜色,字体,粗体,斜体,下划线,嵌入式图像,项目符号列表)。 But I still cannot figure out how I can implement the following requirements: 但是我仍然不知道如何实现以下要求:

  • Control the appereance of the bullet symbol (dot, circle, dash, arrow, star, etc.) 控制项目符号的外观(点,圆,破折号,箭头,星号等)
  • Numbered list (1., 2., 3., etc.) 编号列表(1、2、3等)
  • Table

BTW, I have seen there are several *Span classes out there but I am not sure if they can be any help... And the http://developer.android.com does not provide much useful information about them. 顺便说一句,我已经看到那里有几个* Span类,但是我不确定它们是否有帮助...而且http://developer.android.com没有提供有关它们的很多有用信息。


So, how on earth can I implement a rich text editor on Android? 那么,到底如何在Android上实现富文本编辑器? Can I extends the EditText and add my new functionalities? 我可以扩展EditText并添加新功能吗? Or should I do something from scratch - extends the View and implement everything by myself? 还是我应该从头开始做某事-扩展视图并自己实现所有功能? For later option (extending View), I actually even don't know how to show a text caret and blink it, not mentionging moving the caret with user typing. 对于以后的选项(扩展“视图”),我什至不知道如何显示文本插入符号并使其闪烁,更不用说通过用户键入来移动插入符号了。

I am desperate now... Any hints? 我现在很绝望...有什么提示吗?

Thanks! 谢谢!

-Tony -托尼


( EDIT ) 编辑

After some further investigation, it looks like extending EditText would be my best bet. 经过进一步调查,似乎扩展EditText将是我最好的选择。 I somehow figured out how to use those Span classes and guess I should be able to do most of the tricks by using (extending) them. 我以某种方式弄清楚了如何使用那些Span类,并猜想我应该能够通过使用(扩展)它们来完成大多数技巧。

For example, extending the BulletSpan and overriding drawLeadingMargin should give me the control of the bullet appereances. 例如,扩展BulletSpan并覆盖drawLeadingMargin应该可以控制子弹外观。 Extending the LeadingMarginSpan should help me on the numbered list. 扩展LeadingMarginSpan应该可以帮助我在编号列表上。

As to the table, my initial plan is to extend the LineBackgroundSpan and draw all the table borders in the drawBackground override. 对于表格,我的最初计划是扩展LineBackgroundSpan并在drawBackground替代中绘制所有表格边框。 However, I still need to figure out how to layout all the text in the table cells and properly handle the caret movement and selection. 但是,我仍然需要弄清楚如何在表格单元格中布置所有文本并正确处理插入符号的移动和选择。 Suggestions? 有什么建议吗?

I just published my rich text editor component under the Apache 2.0 license: 我刚刚在Apache 2.0许可下发布了富文本编辑器组件:

https://github.com/1gravity/Android-RTEditor https://github.com/1gravity/Android-RTEditor

在此处输入图片说明

如果需要进行足够大的更改,我可能会同时扩展EditTextTableLayout或者至少最终使用它们的大部分源代码

Can you do the following: 您可以执行以下操作:

  1. Manually hold the contents in the EditText as your own model (ie by seperating and maintaing the content document and the view attributes as seperate entities). 手动将内容作为自己的模型保存在EditText中(即,通过将内容文档和视图属性分离和维护为单独的实体)。
  2. Override the render (or draw method) to do custom layout on parts of the content document (part of your model) that handle non text characters (say bullets with particular attribute). 覆盖渲染(或draw方法)以在内容文档的某些部分(模型的一部分)上处理非文本字符(例如具有特定属性的项目符号)以进行自定义布局。

To me seems like if you have to muck about with the layout, are you better off writing it from scratch on your own. 在我看来,如果您不得不考虑布局问题,那么最好还是自己重新编写。 From what I remember the Edit text (and the richt text editor) is great for anything where you the data is pure text. 从我记得的情况来看,“编辑”文本(和Richt文本编辑器)非常适合任何您将数据作为纯文本的情况。

There is an open source EditText rich text editor called android-richtexteditor but the code was inexplicably deleted in r5. 有一个名为android-richtexteditor的开源EditText RTF编辑器,但是在r5中莫名其妙地删除了代码。 The code is still there in r4 ; 代码仍然在r4中 just use svn to check out r4 or earlier to access it. 只需使用svn签出r4或更早版本即可访问它。 The code appears to support italics, underline, color picker, text size, and more. 该代码似乎支持斜体,下划线,颜色选择器,文本大小等。

Also answered in these questions: 1 , 2 还回答了这些问题: 12

Extend from EditText is a best choice for you, it support CharacterSpan and ParagraphSpan. 从EditText扩展是最适合您的选择,它支持CharacterSpan和ParagraphSpan。

See my App on the Google Play: https://play.google.com/store/apps/details?id=com.hly.notes 在Google Play上查看我的应用程序: https : //play.google.com/store/apps/details?id=com.hly.notes

Check this open source Wordpress mobile application for android.It has very promising Richtexteditor based on Edittext. 检查android.It这个开源 WordPress的移动应用程序具有基于EDITTEXT非常有前途的使用RichTextEditor。

You can download the source from here 您可以从此处下载源代码

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM