2013-01-05 68 views
0

从阅读一些Q &就像在stackoverflow上,我明白webview可能是一个很好的选择来显示epub文件。如何突出显示epub文本android

在下面的线程中,它提到你也可以在webview中高亮显示。 render the epub book in android?

任何人都可以指导我如何在Web视图中突出显示一些文本吗? 在接下来的帖子中,大约2个月前有类似的问题,但还没有得到答案。 https://stackoverflow.com/questions/12948352/highlight-unhighlight-some-text-permanently-in-epub-file-in-android

请帮我弄清楚如何在android中为epub读者实现高亮功能。 谢谢。

+0

你希望能够永久保存更改EPUB文件?我认为这是不可能的,因为'WebView'是只读的......您可能可以使用JavaScript,并随时跟踪更改。 –

+0

谢谢你的回应。我不认为WebView能够保存更改。但基本上,我想要做的就像iPad上的iBook阅读器。我想突出或添加评论给epub。我应该用标签来显示epub文本吗? – codingdaddy

+0

我不认为这会太容易;你将不得不制作自己的epub渲染器/查看器。例如,这是[PDF渲染器](http://andpdf.sourceforge.net/);我不确定epub渲染器如何工作。或者,您可以尝试在“WebView”中使用JavaScript接口来更改文档(也可以以某种方式保存这些更改)。 –

回答

2
  webview.findAll(htmlTextStr); 
     webview.setSelected(true); 
     webview.findNext(true); 

试试这个,htmlTextStr一定是你的文字将被高亮显示

1
 
I am giving this answer based on my experience into development of native epub player for android and ios. 
I had achieved it in this way . 

This is the way I have done text highlighting 

- once your url loads into webview in onload call back inject jquery lib to webview dom like this 


    private void addJQueryJS() 
     { 
      String path = "file:///android_asset/JSLibraries/jquery.min.js"; 
      String data = "{\"MethodName\":\"onJQueryJSLoaded\",\"MethodArguments\":{}}"; 
      String callBackToNative = " jsInterface.callNativeMethod('jstoobjc:"+data+"');"; 
      String script = "function includeJSFile()" 
            +"{" 
            +"function loadScript(url, callback)" 
            +"{" 
            +"var script = document.createElement('script');" 
            +"script.type = 'text/javascript';" 
            +"script.onload = function() {" 
            +"callback();" 
            +"};" 
            +"script.src = url;" 
            +"if(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0])" 
            +"{" 
            +"(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script);" 
            +"}" 
            +"else { callback(); }" 
            +"}" 
            +"loadScript('"+path+"', function()" 
            +"{" 
            +callBackToNative 
            +"});" 
            +"} ; includeJSFile();"; 
      loadUrl("javascript: "+script); 
     } 

wrap each word into a span with a unique id 

then create a js file and inject same as jquery and write touch methods in it 


    function onTouchMove(e) 
    { 
     e.preventDefault(); 
     if((touchendStartStick == true || touchendEndStick == true) && currHVO != undefined) 
     { 
      var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0]; 
      var obj = document.elementFromPoint(touch.pageX-window.pageXOffset,touch.pageY-window.pageYOffset); 

      if($(obj).is('span')) 
      { 
       $(obj).css('background-color','rgba(0,0,255,0.3)'); 
      } 
     } 
    }; 


you can get word x,y width height values also .so you can show start ,end draggable pins on native view. 

you can save highlight by saving the first and last word id of an highlight into db or to some persistent storage.Next time when user opens the book get those words ids and highlight those spans . 

相关问题