问题描述
我需要在 webview 中找到特定的文本,但我无法做到.findall 在 api 级别 16 之后被弃用,我们需要使用 findAllAsync.我们也可以使用 WebView.FindListener.早些时候我使用 findall 并且它工作正常,但它不适用于 kitkat 和棒棒糖.我尝试过 findAllAsync 但我无法找到任何特定文本的计数.我的代码在这里:
I need to find particular text in webview but i can not able to do it.findall is deprecated after api level 16 and we need to use findAllAsync.we can also use WebView.FindListener.Earlier i was used findall and it work fine but it not work in kitkat and lollipop. I have tried with findAllAsync but i can not able to find count of any particular text.my code is here :
@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);
}
谁有demo请提供给我,谢谢
anybody have any demo please provide me, thanks
推荐答案
在您的代码中使用它来搜索所需的字符串.
Use this in your code to search for the string required.
webview.findAllAsync("Your String");
将 FindListener 添加到 WebView
than add FindListener to WebView
webview.setFindListener(new FindListener() {
@Override
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
Toast.makeText(getApplicationContext(), "Matches: " + numberOfMatches, Toast.LENGTH_LONG).show();
}
});
这篇关于在android中的webview中查找特定文本的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!