android - 在 android 的 webview 中查找特定文本的计数

标签 android webview

我需要在 webview 中找到特定的文本,但我做不到。 findall 在 api 级别 16 之后被弃用,我们需要使用 findAllAsync。 我们也可以使用 WebView.FindListener。 早些时候我用过 findall 并且它工作正常但它不适用于 kitkat 和 Lollipop 。我试过使用 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);

}

谁有demo请发给我,谢谢

最佳答案

在您的代码中使用它来搜索所需的字符串。

webview.findAllAsync("Your String");

比将 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();
    }
});

关于android - 在 android 的 webview 中查找特定文本的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29626152/

相关文章:

java - 响应式网站在 Chrome 中以其他方式显示在移动宽度之外的 Web View 上(无响应)看起来不错

ios - 快速的 tableview 到 webview

html - 如何将文本输入区域保留在我的 WebView 底部

android - Android 和 Iphone 之间的应用感知蓝牙 BLE 通信

android - Admob 广告未加载 - 无法加载广告 : 0

android - TestNG结果的testng-results.xml中的 "duration in ms"代表什么?

android - Android 中 TextView、EditText 和按钮的小型大写字母

javascript - Android webview,在 Assets 文件夹中加载javascript文件

java - Android:FragmentManager 版本不匹配

iphone - Objective-C中如何区分链接请求