6

私は「前進」と「後退」を機能させるためにあらゆることを試みました。

更新が機能している[メソッドを「webView.reload();」を読み取るように変更することで理解できました。'webView.refresh();'の代わりに

誰かが前進と後退を手伝うことができますか?「forward」、「canGoForward」、「goForward」、「back」、「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);
}

}

4

1 に答える 1

4

共有したコードには、Webビューを作成してURL間を移動する部分は含まれていません。だから私は何が起こっているのかを推測しているだけです。

webview質問でその部分を示したクラスのインスタンスフィールドのようです。webView新しいページに移動するたびに再作成されているのでしょうか。つまり、コードは次のようになります。

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

これが行われている場合、必要なのは「webView」を一度作成し、loadUrl新しいURLに移動する必要があるたびに呼び出すことだけです。このようにして、webviewインスタンスは履歴を保持できるようになります。

于 2011-06-02T07:28:36.487 に答える