我需要在webview中找到特定的文本,但我无法做到这一点。在api级别16之后不推荐使用findall,我们需要使用findAllAsync。我们还可以使用WebView.FindListener。之前我使用过findall,它可以正常工作,但不适用于kitkat和棒棒糖。我已经尝试过findAllAsync,但是我找不到任何特定文本的计数。我的代码在这里:
@Override
public void onFindResultReceived(int activeMatchOrdinal,
int numberOfMatches, boolean isDoneCounting) {
if (isDoneCounting) {
updateMatchCount(activeMatchOrdinal, numberOfMatches,
numberOfMatches == 0);
}
Log.e("test", "data ::" + numberOfMatches);
}
public void findAll() {
if (webviewdevelopment == null) {
throw new AssertionError(
"No WebView for FindActionModeCallback::findAll");
}
CharSequence find = "Following";
if (0 == find.length()) {
webviewdevelopment.clearMatches();
mMatchesFound = false;
webviewdevelopment.findAll(null);
} else {
mMatchesFound = true;
mNumberOfMatches = 0;
webviewdevelopment.findAllAsync(find.toString());
}
Log.e("test", "data ::" + mNumberOfMatches);
}
public void updateMatchCount(int matchIndex, int matchCount,
boolean isEmptyFind) {
if (!isEmptyFind) {
mNumberOfMatches = matchCount;
mActiveMatchIndex = matchIndex;
updateMatchesString();
} else {
mNumberOfMatches = 0;
}
}
private void updateMatchesString() {
if (mNumberOfMatches == 0) {
} else {
Log.e("test", "data ::" + mNumberOfMatches + ","
+ mActiveMatchIndex + 1 + "," + mNumberOfMatches);
}
}
private void findNext(boolean next) {
if (webviewdevelopment == null) {
throw new AssertionError(
"No WebView for FindActionModeCallback::findNext");
}
if (!mMatchesFound) {
findAll();
return;
}
if (0 == mNumberOfMatches) {
// There are no matches, so moving to the next match will not do
// anything.
return;
}
webviewdevelopment.findNext(next);
updateMatchesString();
}
@Override
public void onFindResultReceived(int activeMatchOrdinal,
int numberOfMatches, boolean isDoneCounting) {
if (isDoneCounting) {
updateMatchCount(activeMatchOrdinal, numberOfMatches,
numberOfMatches == 0);
}
Log.e("test", "data ::" + numberOfMatches);
}
Run Code Online (Sandbox Code Playgroud)
任何人都有演示,请提供我,谢谢
小智 5
在您的代码中使用它来搜索所需的字符串。
webview.findAllAsync("Your String");
Run Code Online (Sandbox Code Playgroud)
而不是将FindListener添加到WebView
webview.setFindListener(new FindListener() {
@Override
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
Toast.makeText(getApplicationContext(), "Matches: " + numberOfMatches, Toast.LENGTH_LONG).show();
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1582 次 |
| 最近记录: |