Android webview后退按钮,无法让它工作

时间:2011-11-15 19:32:07

标签: android webview

我已经创建了我的第一个Android应用程序,它基本上是一个webview,可以在线调用一些页面但是当我按下后退按钮它只关闭应用程序时,如何配置后退按钮以返回页面?

这是我目前的代码:

 package com.mysite.myapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient; 
import android.widget.Button;

public class mysite extends Activity
{   

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

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

    WebView webView; 

    final Activity activity = this;     
    @Override    
    public void onCreate(Bundle savedInstanceState)    
    {        
        super.onCreate(savedInstanceState);        
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);        
        setContentView(R.layout.main);
        final WebView webView = (WebView) findViewById(R.id.webview);        
        webView.getSettings().setJavaScriptEnabled(true);         
        webView.setWebChromeClient(new WebChromeClient() {            
            public void onProgressChanged(WebView view, int progress)            
            {               
                activity.setTitle("Loading...");                
                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)            
            {               
                // Handle the error            
                }             
            @Override            
            public boolean shouldOverrideUrlLoading(WebView view, String url)            
            {                
                view.loadUrl(url);                
                return true;            
                }        
            });         
        webView.loadUrl("http://www.mysite.com/page1.php"); 

        Button webButton = (Button) findViewById(R.id.button1); 
        webButton.setOnClickListener(new OnClickListener() { 
          @Override 
          public void onClick(View v) { 
                 webView.loadUrl("http://www.mysite.com/page2.php"); 
          }});

        Button webButton1 = (Button) findViewById(R.id.button2); 
        webButton1.setOnClickListener(new OnClickListener() { 
          @Override 
          public void onClick(View v) { 
                 webView.loadUrl("http://www.mysite.com/page3.php"); 
          }});

        Button webButton2 = (Button) findViewById(R.id.button3); 
        webButton2.setOnClickListener(new OnClickListener() { 
          @Override 
          public void onClick(View v) { 
                 webView.loadUrl("http://www.mysite.com/page4.php"); 
          }});

        Button webButton3 = (Button) findViewById(R.id.button4); 
        webButton3.setOnClickListener(new OnClickListener() { 
          @Override 
          public void onClick(View v) { 
                 webView.loadUrl("http://www.mysite.com/page4.php"); 
          }});

        Button webButton4 = (Button) findViewById(R.id.button5);
        webButton4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), morescreen.class);
                startActivityForResult(myIntent, 0);
            }

        });

        Button webButton5 = (Button) findViewById(R.id.button6); 
        webButton5.setOnClickListener(new OnClickListener() { 
          @Override 
          public void onClick(View v) { 
                 webView.loadUrl("http://www.mysite.com/page5.php"); 
          }});



        }
}

我真的很感谢你的帮助,我还在学习:)。

提前致谢

4 个答案:

答案 0 :(得分:4)

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
        webview.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

答案 1 :(得分:1)

在您的Activity中,覆盖public和void onBackPressed()方法,并使用内部的webView.goBack()方法返回一页。有关详细信息,请参阅此处developer.android.com/resources/tutorials/views/hello-webview.html。

答案 2 :(得分:1)

检查一下....这篇文章很有用,它会对你有所帮助 http://www.androidaspect.com/2012/09/android-webview-tutorial.html

答案 3 :(得分:0)

使用此:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (wview != null && keyCode == KeyEvent.KEYCODE_BACK && wview.canGoBack() ) {
        wview.goBack();
        return true;
    }
    else
    {
        return super.onKeyDown(keyCode, event);
    }

}