Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当通过Webview打开新链接时,Android Webview的“后退”按钮不起作用_Android_Webview_Android Webview_Back Button_Android Websettings - Fatal编程技术网

当通过Webview打开新链接时,Android Webview的“后退”按钮不起作用

当通过Webview打开新链接时,Android Webview的“后退”按钮不起作用,android,webview,android-webview,back-button,android-websettings,Android,Webview,Android Webview,Back Button,Android Websettings,我有一个带有本地html、css和JavaScript文件的android应用程序 其中一个页面包含一个图像列表,这些图像有一个连接到外部网站的链接 每当我点击链接时,URL就会在webview中打开。但是,当我单击“上一步”按钮时,它不会返回到上一页 它适用于除使用外部链接的页面之外的所有页面 MainActivity.java如下所示: public class MainActivity extends Activity { WebView mWebView; @Override pro

我有一个带有本地html、css和JavaScript文件的android应用程序

其中一个页面包含一个图像列表,这些图像有一个连接到外部网站的链接

每当我点击链接时,URL就会在webview中打开。但是,当我单击“上一步”按钮时,它不会返回到上一页

它适用于除使用外部链接的页面之外的所有页面

MainActivity.java如下所示:

public class MainActivity extends Activity {

WebView mWebView;

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


    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.loadUrl("file:///android_asset/www/index.html");

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mWebView.setWebViewClient(new WebViewClient());
    // Force links and redirects to open in the WebView instead of in a browser
}

@Override
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.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);
}
}

MyAppWebViewClient.java如下所示:

public class MyAppWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (Uri.parse(url).getHost().equals("/menu")) {
        return true;
    }

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    view.getContext().startActivity(intent);
    return true;
}

}看看我在我的应用程序中做了什么,你也可以使用这个

@Override
    public void onBackPressed() {

        if (webView.canGoBack()) {

            webView.goBack();

        } else {
            if (backPressed) {
                super.onBackPressed();
                return;
            }

            this.backPressed = true;
            Toast.makeText(this, "Press Back One More Time To Exit", Toast.LENGTH_SHORT).show();

            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    backPressed = false;
                }
            }, 2000);

        }

    }
在此代码中,如果您的webView可以返回,它将返回,但如果不能返回,将出现一个祝酒词,要求用户再次按back退出,以防止意外的按back

Handler.postDelayed()用于设置2秒的时间限制…两秒后,再次提示用户按back