【问题标题】:search in html file and show it in the list在 html 文件中搜索并显示在列表中
【发布时间】:2014-08-30 16:43:20
【问题描述】:

我正在尝试在存储在我的项目资产文件夹中的所有 html 文件中搜索一个单词。但是我的方法只是在当前的html文件中搜索。

findBox = new EditText(this);
    findBox.setMinEms(30);
    findBox.setSingleLine(true);
    findBox.setHint("Ara...");
    findBox.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && ((keyCode == KeyEvent.KEYCODE_ENTER))) {
                wv.findAll(findBox.getText().toString());
                try {
                    // Can't use getMethod() as it's a private method
                    for (Method m : WebView.class.getDeclaredMethods()) {
                        if (m.getName().equals("setFindIsUp")) {
                            m.setAccessible(true);
                            m.invoke(wv, true);
                            break;
                        }
                    }
                } catch (Exception ignored) {
                }
            }

            return false;
        }
    });

【问题讨论】:

  • But my method is only searching in current html file. 应该如此。 WebView.findAll() 搜索当前文档。您的代码显然对资产没有任何作用。

标签: android search webview


【解决方案1】:

我认为您误解了 WebView 的工作原理。当您加载带有以下内容的 html 文档时:

WebView wv;
// ... some initialization here
wv.loadUrl("file:///android_asset/page.html");

它只加载一个特定页面,它不会帮助您在资产中的所有 html 文件中查找内容。

关于搜索所有文件:

1) 获取资产目录或其中的某个子目录中的所有文件名。在下面的示例中,我获取了 /assets/html 中的所有文件:

String dirFrom = "html"
AssetManager am = res.getAssets();
String fileList[] = am.list(dirFrom);

2) 为每个文件打开一个 InputStream 并使用它来搜索其内容,类似于此

AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");

3) 如果您想在 WebView 中显示所有内容,那么您将知道文件名、它们的位置和 ofc,现在您可以使用 loadUrl 加载每个文件。

在旁注中,我注意到您正在使用:

public int findAll(字符串查找)

你应该:

public void findAllAsync(字符串查找)

【讨论】:

  • 感谢您的回答。我可以用你的算法搜索所有文件。实际上,我想用它们的文件名存储我们在 html 文件中找到的单词并显示一个列表视图。但是 findAllAsync() 是不够的。你能帮我解决这个问题吗?
  • - 扫描assets 目录中的所有html 文件以搜索字符串- 在ListView 中显示哪些文件包含搜索结果 WebView 的方法findAllAsync() 用于执行浏览器- 像发现。就像在 Firefox 中说的那样,您使用 ctrl-F 来查找某些内容,它允许您在结果之间跳转。除了找到的结果数量 + findNext() 之外,它不会返回任何信息,将 WebView 滚动到下一个结果。
猜你喜欢
  • 2019-09-22
  • 2018-03-13
  • 1970-01-01
  • 1970-01-01
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-29
  • 1970-01-01
相关资源
最近更新 更多