gpt4 book ai didi

android - 在代码中使用 findAllAsync 两次

转载 作者:太空狗 更新时间:2023-10-29 14:49:25 26 4
gpt4 key购买 nike

我想搜索一个网页来找到两个字符串。我使用这段代码:

webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
webView.findAllAsync("str1");
webView.findAllAsync("str2");
}
});

和这个 FindListener :

public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
if(numberOfMatches > 0)
(...) //do something
}

但在这种情况下,onFindResultReceived 仅针对 str2 调用,并且如果 str1numberOfMatches 大于0 什么都没做。是什么原因,应该如何解决?

最佳答案

来自 the docs

Finds all instances of find on the page and highlights them, asynchronously. Notifies any registered WebView.FindListener. Successive calls to this will cancel any pending searches.

换句话说,您的第二个调用取消了第一个。解决方案是等到每个搜索完成后再开始下一个。下面是一个可以为任意数量的字符串实现所需结果的示例

private int mCurrentSearchIndex = -1;
private String mSearchTerms = { "str1", "str2" }

webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
nextSearch();
}
});

private void nextSearch() {
mCurrentSearchIndex ++; //the fisrt tiem this is called the index gets set to 0
if (mCurrentSearchIndex < mSearchTerms.length) {
//get the search string corresponding to this index and then search it
webView.findAllAsync(mSearchTerms[mCurrentSearchIndex]);
}
}

public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
if (isDoneCounting) {
if (numberOfMatches > 0) {
...
}
//here we know the previous find finished, so its safe to start another
nextSearch();
}
}

关于android - 在代码中使用 findAllAsync 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36592208/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com