WebView 后退、刷新、前进?根本行不通!

发布于 2024-11-12 05:02:32 字数 1534 浏览 3 评论 0原文

我已尽一切努力让我的“前进”和“后退”发挥作用。

刷新正在工作[我通过更改读取“webView.reload();”的方法来解决这个问题而不是“webView.refresh();”

有人可以帮忙前进和后退吗?我尝试过“前进”、“canGoForward”和“goForward”以及“后退”、“canGoBack”和“goBack”。没有错误代码,但这些方法实际上都没有做任何事情。

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu); // Add menu items, second value is the id, use this in the onCreateOptionsMenu
    menu.add(0, 1, 0, "Back");
    menu.add(0, 2, 0, "Refresh");
    menu.add(0, 3, 0, "Forward");
    return true; // End of menu configuration
}
public boolean onOptionsItemSelected(MenuItem item){ // Called when you tap a menu item
    switch (item.getItemId()){
        case 1: //If the ID equals 1, go back
            webView.goBack();
        return true;
        case 2 : //If the ID equals 2, refresh
            webView.reload();
        return true;
        case 3: //If the ID equals 3, go forward
            webView.goForward();
        return true;
        }
    return false;
    }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { // Enables browsing to previous pages with the hardware back button
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { // Check if the key event was the BACK key and if there's history
        webView.goBack();
        return true;
    }   // If it wasn't the BACK key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

}

I have tried everything to get my 'forward' and 'back' to work.

Refresh is working [I figured it out by changing the method to read 'webView.reload();' instead of 'webView.refresh();'

Can anyone assist with the forward and back? I have tried 'forward', 'canGoForward' and 'goForward' as well as 'back', 'canGoBack' and 'goBack'. There are no error codes however none of those methods actually do anything.

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu); // Add menu items, second value is the id, use this in the onCreateOptionsMenu
    menu.add(0, 1, 0, "Back");
    menu.add(0, 2, 0, "Refresh");
    menu.add(0, 3, 0, "Forward");
    return true; // End of menu configuration
}
public boolean onOptionsItemSelected(MenuItem item){ // Called when you tap a menu item
    switch (item.getItemId()){
        case 1: //If the ID equals 1, go back
            webView.goBack();
        return true;
        case 2 : //If the ID equals 2, refresh
            webView.reload();
        return true;
        case 3: //If the ID equals 3, go forward
            webView.goForward();
        return true;
        }
    return false;
    }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { // Enables browsing to previous pages with the hardware back button
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { // Check if the key event was the BACK key and if there's history
        webView.goBack();
        return true;
    }   // If it wasn't the BACK key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故乡的云 2024-11-19 05:02:32

您共享的代码不包括您创建 webview 和在 url 之间导航的部分。所以我只是猜测可能会发生什么。

看来 webview 是您的类的实例字段,您已在问题中显示了其部分。是否每次导航到新页面时都会重新创建 webView?也就是说,代码类似于:

webview = new WebView(this);
webview.loadUrl("http://slashdot.org/");

如果这就是正在做的事情,您所需要做的就是创建一次“webView”,然后每次需要导航到新的 url 时调用 loadUrl 。这样 webview 实例将能够保留历史记录。

The code you shared doesn't include the portion where you are creating webview and navigating between urls. So I am just taking a guess at what might be happening.

It seems that webview is an instance field of your class whose portion you have shown in the question. Could it be that webView is being recreated every time you navigate to a new page? That is, the code is something like:

webview = new WebView(this);
webview.loadUrl("http://slashdot.org/");

If this is what is being done, all you need is to create the 'webView' once and just call loadUrl everytime you need to navigate to new url. This way the webview instance will be able to retain the history.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文