【问题标题】:How to capture text in first line of current screen in Android?如何在Android当前屏幕的第一行捕获文本?
【发布时间】:2016-04-03 04:53:03
【问题描述】:

我的项目是一个电子书阅读器应用程序。屏幕中只有一个 VebView 并从 html 文件中获取数据。它只有文本内容。我需要在当前屏幕的第一行获取文本。在下一步中,我需要在文档中找到文本并获取文本在屏幕上的位置。最后,我需要滚动找到的位置。解决该问题所需的一切:(WebView Text Zoom Issue in Android)

html内容:

<!DOCTYPE html>

    <head>
        <style type="text/css"> 
            p{} p.x1{} p.x2{} p.x3{} p.x4{} 
            h2.x1{} h2.x2{} h2.x3{} h2.x4{}
        </style>
    </head>

    <body>

        //paragraph-1
        <p class="x1">Title-1</p>
        <p class="x2">Title-2</p>
        <p class="x3">Title-3</p>
        <p class="x4">Title-4</p>
        <p>Text content.</p>


        //paragraph-2
        <h class="x1">Title-1</p>
        <h class="x2">Title-2</p>
        <p>Text content.</p>


        //paragraph-3
        <h class="x3">Title-1</p>
        <h class="x4">Title-2</p>
        <p class="x3">Title-3</p>
        <p>Text content.</p>


        //paragraph-4
        <h class="x2">Title-1</p>
        <p class="x3">Title-2</p>
        <p>Text content.</p>


        //...

    </body>

</html>

【问题讨论】:

  • 如果你不需要渲染它,那为什么要使用 WebView?只需发出原始 HTTP(S) 请求。

标签: javascript java android android-layout webview


【解决方案1】:

我想你正在寻找这个?

function findTopObject (elements) {
    var top = Number.POSITIVE_INFINITY;
    var obj = null;

    Array.from(elements).forEach(function (o) {
        var t = o.getBoundingClientRect(o).top;
        if (t > 0.0 && top > t) {
            obj = o;
            top = t;
        }
    });

    return obj;
}

for (var i = 0; i < 1000; i++) {
  var p = document.createElement("p");
  p.innerText = "Line " + i;
  document.body.appendChild(p);
}

var elements = document.querySelectorAll("p");
var obj = null;

window.onscroll = function() {
  if (obj != null) {
    obj.style.backgroundColor = "";
  }

  obj = findTopObject(elements);

  if (obj != null) {
    obj.style.backgroundColor = "red";
  }
};

function findTopObject(elements) {
  var top = Number.POSITIVE_INFINITY;
  var obj = null;
  Array.from(elements).forEach(function(o) {
    var t = o.getBoundingClientRect(o).top;
    if (t > 0.0 && top > t) {
      obj = o;
      top = t;
    }
  });
  return obj;
}
&lt;body&gt;&lt;/body&gt;

【讨论】:

    【解决方案2】:

    没有直接的方法可以做你想做的事, 首先通过做得到html内容

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    HttpResponse response = client.execute(request);
    
    String html = "";
    InputStream in = response.getEntity().getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder str = new StringBuilder();
    String line = null;
    while((line = reader.readLine()) != null)
    {
        str.append(line);
    }
    in.close();
    html = str.toString();
    

    然后使用子字符串概念得到你想要的,看看Get Html content

    或者你可以使用Jsoap parser 从 HTML 中的指定标签获取值

    Document doc=Jsoup.connect("http://www.yahoo.com").get();
    Elements elements=doc.select("img");
    
    for(Element e:elements)
     {
      System.out.println(e.attr("src"));
     }
    

    【讨论】:

    • html 文件混合了许多

      ...

      标签。我不想全部,我只想在当前屏幕上获得第一行
    • 你能告诉我们你的 html 源代码吗?我们可以弄清楚你想要什么?
    猜你喜欢
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    相关资源
    最近更新 更多