在 WebView Android 上实现后退按钮 [英] Implement Back Button on WebView Android

查看:30
本文介绍了在 WebView Android 上实现后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在android webview上实现后退按钮,但它说

I tried to implement back button on android webview but it said that

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 上工作

I tried approach from other source on stackoverflow but still not get a valid answer. Maybe my knowledge still not par with other. I hope that someone in this community can help me to solve this issue. Also a brief explanation about what should I do to make the back button work on webview

推荐答案

WebView 外部和 Activity 内部使用 onBackPressed() 方法类方法:

Use onBackPressed() method outside of WebView and inside the Activity class method:

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

}

另外,尝试在 onCreate() 方法中初始化 WebView 并删除 onBackPressed() 中的第二个重复代码.

Also, try initializing WebView inside onCreate() method and remove the second duplicate code inside onBackPressed().

为了能够在整个Activity中访问它,像这样初始化它:

To be able to access it inside whole Activity, initialize it like this:

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); 
}

这篇关于在 WebView Android 上实现后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆