在WebView Android上实施后退按钮

时间:2018-09-17 17:18:29

标签: java android webview

我试图在android webview上实现后退按钮

error: method does not override or implement a method from a supertype    
error: cannot find symbol method onBackPressed()

活动代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ProgressBar;
import android.view.KeyEvent;

public class MainActivity extends AppCompatActivity {

    String ShowOrHideWebViewInitialUse = "show";
    private WebView webview ;
    private ProgressBar spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webview =(WebView)findViewById(R.id.webView);
        spinner = (ProgressBar)findViewById(R.id.progressBar1);
        webview.setWebViewClient(new CustomWebViewClient());

        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
        webview.loadUrl("http://web.com");

    }

    // This allows for a splash screen
    // (and hide elements once the page loads)
    private class CustomWebViewClient extends WebViewClient {

        @Override
        public void onPageStarted(WebView webview, String url, Bitmap favicon) {

            // only make it invisible the FIRST time the app is run
            if (ShowOrHideWebViewInitialUse.equals("show")) {
                webview.setVisibility(webview.INVISIBLE);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            ShowOrHideWebViewInitialUse = "hide";
            spinner.setVisibility(View.GONE);

            view.setVisibility(webview.VISIBLE);
            super.onPageFinished(view, url);

        }

        @Override
        public void onBackPressed()
        {
            WebView webView = (WebView) findViewById(R.id.webView);
            if(webView.canGoBack()){
                webView.goBack();
            }else{
                super.onBackPressed();
            }
        }
    }
}

我在stackoverflow上尝试了其他来源的方法,但仍然没有得到有效的答案。也许我的知识还不能与其他知识相提并论。我希望这个社区中的某人可以帮助我解决这个问题。另外简要说明了如何使后退按钮在webview上工作

1 个答案:

答案 0 :(得分:0)

select count(*) from '||var_Table into var_NumRec; 外部和onBackPressed()类方法内部使用WebView方法:

Activity

此外,请尝试在@Override public void onBackPressed() { WebView webView = (WebView) findViewById(R.id.webView); // Remove this if(webView.canGoBack()){ webView.goBack(); }else{ super.onBackPressed(); } } } 方法内初始化WebView,并删除onCreate()内部的第二个重复代码。


要能够在整个onBackPressed()内部访问它,请像这样初始化它:

Activity