Android 3.x 仅 WebView 文本选择 + JavaScript
问题域:基于 Android WebView 的 ePub 格式阅读器 -
我们需要可通过 JavaScript 方法访问的文本突出显示(即打开、关闭、保存、通过电子邮件发送等)
任何知情者,如果我是这样,请纠正我错误的: 在 WebView 上的文本选择过程中——
基本上我们会得到一个 TextView 覆盖层(WebTextView),它除了视觉上之外绝不会附加到底层 html(顺便说一句,将其全部排列并使其无缝,做得很好),我们在其上执行 TextSelectionActionMode 并向 ClipboardManager 提供结果...为了明确起见,在此库存过程中的任何时间实际上并未选择带有 WebView 的实际 HTML,这就是 JavaScript 无法访问操作结果的原因。
因此,为了使这种行为在 3.1+ 中发挥作用,我有一些可能的计划来在 WebView 上实现我自己的选择 - 这是我想知道我是否无法看到角落之外的部分:) - -
1)捕获启动选择模式的长按的MOTION_DOWN,覆盖内置的TextSelection ActionMode(以提供客户品牌——我发现这样做的方式太黑客了,很伤人)观察拖动,捕获MOTION_UP位置,将这些坐标映射到 WebView,并根据坐标进行选择(同时将文本与 ClipboardManager 捕获的文本进行比较)
2)创建我自己的“WebTextView”(整个工作哈哈)并且不那么黑客地创建我自己的 ActionMode 并执行所有操作以上。
3) 使用股票选择过程生成的 MotionEvents 并以某种方式覆盖由此触发的股票 TextSelection ActionMode。
这些是我看到的选项,希望是我的接近和沮丧 这让我对一个更简单的东西视而不见(将其视为内置) 解决方案或两个。
再次感谢您抽出时间。
Problem domain: Android WebView based ePub format reader --
We need text highlights which are accessible via JavaScript methods (ie turn em on, turn em off, save them email them, etc)
Anyone in the know, please correct me if I'm wrong:
During text selection on a WebView --
Basically we are handed a TextView overlay (WebTextView) which is IN NO WAY attached to the underlying html other than visually (nice job on lining it all up and making it seamless, btw), on which we perform the TextSelectionActionMode and the ClipboardManager is supplied with the results... to make that clear, the actual HTML with the WebView IS NOT actually selected at any time during this stock process, which is why the JavaScript cannot access the results of the action.
So in order to make this behavior work in 3.1+, I have a few possible plans going forward to implement my own selection on a WebView -- this is the part where I'm wondering if I cannot see out of my corner :) --
1) capture MOTION_DOWN of the long click which initiates selection mode, override the built in TextSelection ActionMode( to provde the clients branding -- the way I found to do so is so hackish it hurts) watch the drag, capture MOTION_UP position, map these coords onto the
WebView, and make my selection based on the coords (while comparing the text to that captured by the ClipboardManager)
2) create my own "WebTextView" (whole lotta work lol) and not-so- hackishly create my own ActionMode and do all of the above.
3) use the MotionEvents generated by the stock selection process and somehow override the stock TextSelection ActionMode triggered by this.
These are the options I see, hopefully my proximity and frustration
with this have blinded me to a simpler (read that as built-in)
solution or two.
Thanks again for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 2.2 - 4.0.3 中可以正常工作。我使用了 javascript 接口并将所有触摸传递给它。该解决方案似乎工作得很好,我已经在 github 上放了一个示例项目。 github 项目在资产文件夹中包含必要的 js,以及加载测试页面并实现处理选择的所有必要方法的测试页面和 Web 视图。 github 项目的链接为 https://github.com/btate/BTAndroidWebViewSelection。努力吧。
I got this working in 2.2 - 4.0.3. I used a javascript interface and passed all touches to it. The solution seems to work pretty well and I've put an example project on github. The github project includes the necessary js in the assets folder as well as a test page and web view that loads the test page and implements all necessary methods to handle the selection. The link to the github project is https://github.com/btate/BTAndroidWebViewSelection. Have at it.
我不确定什么方法/属性可以为您提供范围和选择。我在文档中没有看到任何内容。但我能够使用反射获取选定的像素范围。不确定这是否对任何人有帮助,但认为值得在这里发布。
希望这对某人有帮助
编辑:这不适用于 3.1+
I'm not 100% sure what methods/properties give you the Range and Selection. I didn't see any in the documentation. But I was able to get the selected range in pixels using reflection. Not sure if this helps anybody, but thought it was worth posting here.
Hopefully that helps somebody
EDIT: This doesn't work on 3.1+