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

发布于 2024-10-04 16:50:44 字数 1505 浏览 2 评论 0原文

我想知道是否有任何好的选择可以在 Android 中实现富文本编辑器。请注意,我所说的是一种可在 Android 应用程序中使用的富文本编辑器,而不是使用 HTML 和 Javascript 嵌入网页中的编辑器。

我的要求是:

  • 基本格式(颜色、字体、突出显示、粗体、斜体、下划线等)
  • 超链接
  • 内联图像
  • 项目符号列表和编号列表
  • 内联表格(只有单元格内的内容是可编辑的,

而不是表格结构 )看,这与 Windows 上典型的 RichEdit 控件非常相似。

以下是我迄今为止所做的一些努力(调查和原型设计):

使用 WebView

我尝试使用 WebView 控件通过一个 .内容变得可编辑,并且由于它是 HTML,我想它可以满足我的大部分要求。但它有几个问题:(

  • 致命的)没有文本插入符。用户将不知道他/她键入的字符将被插入到哪里。
  • 默认情况下,屏幕软键盘不可见。有一个技巧是用户必须长按菜单按钮才能调出键盘。但我认为这是一个非常糟糕的用户体验。此外,屏幕布局未正确重新排列,文本插入点有时会被键盘覆盖。

使用 EditText

我尝试过使用 EditText 控件。它似乎支持某种程度的富文本编辑(颜色、字体、粗体、斜体、下划线、内联图像、项目符号列表)。但我仍然不知道如何实现以下要求:

  • 控制项目符号的外观(点、圆、破折号、箭头、星等)
  • 编号列表(1.、2.、3.等)

顺便说一句,我已经看到有几个 *Span 类,但我不确定它们是否有任何帮助......并且 http://developer.android.com 没有提供太多有关它们的有用信息。


那么,到底如何在Android上实现富文本编辑器呢? 我可以扩展 EditText 并添加新功能吗?或者我应该从头开始做一些事情 - 扩展视图并自己实现所有内容?对于后面的选项(扩展视图),实际上我什至不知道如何显示文本插入符并使其闪烁,更不用说通过用户输入来移动插入符了。

我现在很绝望...有什么提示吗?

谢谢!

-Tony


编辑

经过进一步调查,看来扩展 EditText 将是我最好的选择。我以某种方式弄清楚了如何使用这些 Span 类,并猜测我应该能够通过使用(扩展)它们来完成大部分技巧。

例如,扩展 BulletSpan 和覆盖 drawLeadingMargin 应该可以让我控制项目符号的外观。扩展LeadingMarginSpan 应该对我在编号列表上有所帮助。

对于表格,我最初的计划是扩展 LineBackgroundSpan 并在 drawBackground 覆盖中绘制所有表格边框。但是,我仍然需要弄清楚如何布局表格单元格中的所有文本并正确处理插入符移动和选择。建议?

I am wondering if there are any good options to implement a rich text editor in 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.

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.

Here are some efforts (investigation & prototyping) I have made so far:

Using WebView

I have tried using a WebView control to load an HTML fragment with one . The content becomes editable and as it is HTML, I suppose it can meet most of my requirements. 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

I have tried using the EditText control. 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.)
  • 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.


So, how on earth can I implement a rich text editor on Android?
Can I extends the EditText and add my new functionalities? 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. 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.

For example, extending the BulletSpan and overriding drawLeadingMargin should give me the control of the bullet appereances. Extending the LeadingMarginSpan should help me on the numbered list.

As to the table, my initial plan is to extend the LineBackgroundSpan and draw all the table borders in the drawBackground override. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

阪姬 2024-10-11 16:50:44

我刚刚在 Apache 2.0 许可证下发布了我的富文本编辑器组件:

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

在此处输入图像描述

I just published my rich text editor component under the Apache 2.0 license:

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

enter image description here

断爱 2024-10-11 16:50:44

您可以执行以下操作:

  1. 手动将 EditText 中的内容保存为您自己的模型(即,通过将内容文档和视图属性作为单独的实体进行分离和维护)。
  2. 重写渲染(或绘制方法)以对处理非文本字符(例如具有特定属性的项目符号)的内容文档部分(模型的一部分)进行自定义布局。

在我看来,如果您必须对布局进行修改,那么您最好自己从头开始编写它。据我所知,编辑文本(和富文本编辑器)非常适合任何数据为纯文本的情况。

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).
  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).

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.

鹊巢 2024-10-11 16:50:44

我可能会扩展 EditTextTableLayout 或至少最终使用它们的大部分 如果我需要进行足够大的更改。

I would probably extend both EditText and TableLayout or at least end up using most of their source if there were big enough changes I needed to make.

一指流沙 2024-10-11 16:50:44

有一个名为 android-richtexteditor 的开源 EditText 富文本编辑器,但代码是r5中莫名其妙被删除了。 代码是仍然存在于 r4 中;只需使用 svn 签出 r4 或更早版本即可访问它。该代码似乎支持斜体、下划线、颜色选择器、文本大小等。

还回答了这些问题:12

There is an open source EditText rich text editor called android-richtexteditor but the code was inexplicably deleted in r5. The code is still there in r4; just use svn to check out r4 or earlier to access it. The code appears to support italics, underline, color picker, text size, and more.

Also answered in these questions: 1, 2

箜明 2024-10-11 16:50:44

从EditText扩展是您的最佳选择,它支持CharacterSpan和
段落跨度。

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

Extend from EditText is a best choice for you, it support CharacterSpan and
ParagraphSpan.

See my App on the Google Play:
https://play.google.com/store/apps/details?id=com.hly.notes

鹤仙姿 2024-10-11 16:50:44

检查这个开源 Android Wordpress mobile 应用程序。它具有基于 Edittext 的非常有前景的 Richtexteditor

您可以从此处下载源代码

,谢谢

Check this open source Wordpress mobile application for android.It has very promising Richtexteditor based on Edittext.

You can download the source from here

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文