如何回到previous页面,如果后退按钮是pssed中的WebView $ P $? [英] How to go back to previous page if back button is pressed in WebView?

查看:111
本文介绍了如何回到previous页面,如果后退按钮是pssed中的WebView $ P $?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序中,我有一个的WebView 在这里我展示一些网站。它的工作原理,点击网页的链接进入下一个页面里面我的应用程序的网站。但是,当我点击手机的返回按钮,它需要我直入我的应用程序。我想回到previous页面在网站上代替。我怎样才能做到这一点?

下面是我使用的code样品:<​​/ P>

 公共类Webdisplay延伸活动{

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);

        。this.getWindow()requestFeature(Window.FEATURE_PROGRESS);
        的setContentView(R.layout.webdisplay);

        getWindow()。setFeatureInt(Window.FEATURE_PROGRESS,
                Window.PROGRESS_VISIBILITY_ON);

        吐司loadingmess = Toast.makeText(这一点,
                Cargando厄尔尼诺日报霍伊,Toast.LENGTH_SHORT);
        loadingmess.show();

        的WebView myWebView;

        myWebView =(web视图)findViewById(R.id.webview);
        myWebView.getSettings()setJavaScriptEnabled(真)。
        myWebView.loadUrl(http://www.elsalvador.com);
        myWebView.setWebViewClient(新WebViewClient());
        myWebView.setInitialScale(1);
        。myWebView.getSettings()setBuiltInZoomControls(真正的);
        。myWebView.getSettings()setUseWideViewPort(真正的);

        最后活动MyActivity =这一点;
        myWebView.setWebChromeClient(新WebChromeClient()
        {
            公共无效onProgressChanged(web视图来看,INT进度)
            {
                MyActivity.setTitle(载入中...);
                MyActivity.setProgress(进度* 100);

                如果(进度== 100)
                    MyActivity.setTitle(R.string.app_name);
            }
        });
    }
}
 

解决方案

我用这样的事情在我的活动WebViews:

  @覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
    如果(event.getAction()== KeyEvent.ACTION_DOWN){
        开关(钥匙code){
            案例KeyEvent.KEY code_BACK:
                如果(mWebView.canGoBack()){
                    mWebView.goBack();
                } 其他 {
                    完();
                }
                返回true;
        }

    }
    返回super.onKeyDown(键code,事件);
}
 

编辑:

有关此code的工作,你需要一个字段添加到活动包含web视图:

 私人的WebView mWebView;
 

初​​始化它在的onCreate()方法,你应该是好去。

  mWebView =(web视图)findViewById(R.id.webView);
 

I have an app in which I have a WebView where I display some websites. It works, clicking a link in the webpage goes to the next page in the website inside my app. But when I click the phone's back button, it takes me straight into my app. I want to go back to the previous page in the website instead. How can I do this?

Here is the code sample I'm using:

public class Webdisplay extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.webdisplay);

        getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
                Window.PROGRESS_VISIBILITY_ON); 

        Toast loadingmess = Toast.makeText(this,
                "Cargando El Diario de Hoy", Toast.LENGTH_SHORT);
        loadingmess.show();

        WebView myWebView;

        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl("http://www.elsalvador.com");
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.setInitialScale(1);
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.getSettings().setUseWideViewPort(true);

        final Activity MyActivity = this;
        myWebView.setWebChromeClient(new WebChromeClient() 
        {
            public void onProgressChanged(WebView view, int progress)   
            {
                MyActivity.setTitle("Loading...");
                MyActivity.setProgress(progress * 100); 

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

解决方案

I use something like this in my activities with WebViews:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                } else {
                    finish();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

Edit:

For this code to work, you need to add a field to the Activity containing the WebView:

private WebView mWebView;

Initialize it in the onCreate() method and you should be good to go.

mWebView = (WebView) findViewById(R.id.webView);

这篇关于如何回到previous页面,如果后退按钮是pssed中的WebView $ P $?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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