問題描述
我想知道是否有任何好的選擇可以在 Android 中實現 富文本編輯器.請注意,我說的是可以在 Android 應用程序中使用的富文本編輯器,而不是使用 HTML 和 Javascript 嵌入網頁中的編輯器.
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.
我的要求是:
- 基本格式(顏色、字體、突出顯示、粗體、斜體、下劃線等)
- 超鏈接
- 內嵌圖片
- 項目符號列表和編號列表
- 內嵌表格(只有單元格內的內容是可編輯的,而不是表格結構)
如您所見,這與 Windows 上的典型 RichEdit 控件非常相似.
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:
我嘗試使用 WebView 控件加載一個帶有 .內容變得可編輯,因為它是 HTML,我想它可以滿足我的大部分要求.但它有幾個問題:
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:
- (致命)沒有文本插入符號.用戶將不知道他/她輸入的字符將被插入到哪里.
- 默認情況下,屏幕軟鍵盤是不可見的.有一個技巧,用戶必須長按菜單按鈕才能調出鍵盤.但我認為這是一個非常糟糕的用戶體驗.此外,屏幕布局未正確重新排列,文本插入點有時會被鍵盤覆蓋.
我嘗試過使用 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:
- 控制項目符號符號的外觀(點、圓、破折號、箭頭、星號等)
- 編號列表(1.、2.、3. 等)
- 表格
順便說一句,我已經看到那里有幾個 *Span 類,但我不確定它們是否可以提供任何幫助......以及 http://developer.android.com 沒有提供太多有用的信息.
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.
那么,究竟如何在 Android 上實現富文本編輯器呢?我可以擴展 EditText 并添加我的新功能嗎?還是我應該從頭開始做一些事情 - 擴展視圖并自己實現一切?對于后面的選項(擴展視圖),我什至不知道如何顯示文本插入符號并使其閃爍,更不用說在用戶輸入時移動插入符號了.
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?
謝謝!
-托尼
經過進一步調查,看起來擴展 EditText 是我最好的選擇.我不知何故想出了如何使用這些 Span 類,并猜測我應該能夠通過使用(擴展)它們來完成大部分技巧.
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.
例如,擴展 BulletSpan 并覆蓋 drawLeadingMargin 應該可以讓我控制項目符號的外觀.擴展LeadingMarginSpan 應該對我的編號列表有所幫助.
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.
對于表格,我最初的計劃是擴展 LineBackgroundSpan 并在 drawBackground 覆蓋中繪制所有表格邊框.但是,我仍然需要弄清楚如何布局表格單元格中的所有文本并正確處理插入符號的移動和選擇.有什么建議嗎?
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?
推薦答案
我剛剛在 Apache 2.0 許可下發布了我的富文本編輯器組件:
I just published my rich text editor component under the Apache 2.0 license:
https://github.com/1gravity/Android-RTEditor
這篇關于在 Android 中實現富文本編輯器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!