【问题标题】:How to add back button to Android Web App如何将返回按钮添加到 Android Web App
【发布时间】:2012-04-19 12:24:01
【问题描述】:

我正在尝试整理一个网络应用程序,但找不到一种可能的方法来包括通过手机的软键使用后退按钮。我该怎么做呢? 即我想使用手机上的后退按钮返回上一个查看的网页。

谢谢

乔丹

  package com.wear2gym;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class Wear2gym extends Activity
{
    final Activity activity = this;



    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
        WebView webView = (WebView) findViewById(R.id.WebView);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Pumping some iron...");
                activity.setProgress(progress * 100);

                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                 Toast.makeText(activity, "Sorry but there is no internet connection! " , Toast.LENGTH_LONG).show();
                 view.loadUrl("file:///android_asset/nointernet.html");
                // Handle the error
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });

        webView.loadUrl("http://wear2gym.co.uk");
        webView.canGoBack();
    }
}

【问题讨论】:

    标签: android eclipse web-applications webview


    【解决方案1】:

    我不推荐 onBackPressed(),因为它仅在 API 级别 5 后可用

    你会在这里找到很棒的信息:http://developer.android.com/guide/webapps/webview.html

    @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) && myWebView.canGoBack() {
            myWebView.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);
    }
    

    【讨论】:

    • 您好!感谢您的回复。添加这个的最佳位置在哪里?抱歉,我是 Android 开发领域的新手!
    • 就在最后一个括号 } 之前。另外,请不要忘记投票和/或接受答案,这就是本网站的运作方式。
    【解决方案2】:

    覆盖onBackPressed() 方法:

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

    这将返回WebView,直到无法返回,在这种情况下它将退出Activity

    【讨论】:

    • @Profete162 正确,但大多数应用的目标大于 5(只有 1% 的活动设备低于 API 级别 5)
    • 为数百万台设备中的 1% 更改一行代码似乎很有趣 ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2015-06-26
    • 2019-01-01
    相关资源
    最近更新 更多