我的projec是一个电子书阅读器应用程序。屏幕上只有一个VebView,并从html文件中获取数据。它只有文本内容。我需要在当前屏幕的第一行得到文本。下一步,我需要在文档中找到文本,并获取文本的屏幕位置。最后,我需要滚动找到的位置。解决这个问题所必需的一切:(安卓中的WebView文本缩放问题)
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>发布于 2016-04-03 13:58:07
我觉得你在找这个?
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;
}<body></body>
发布于 2016-04-03 05:05:46
没有直接的方法来做您想做的事情,首先通过以下操作获得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();然后使用子字符串conccept获取您想要的东西,请看一下获取Html内容
也可以使用Jsoap解析器从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"));
}https://stackoverflow.com/questions/36381742
复制相似问题