Android webView应用程序 - 无法返回按钮返回页面

时间:2013-11-16 12:55:49

标签: java android eclipse webview

我对Android编码有点新,所以我在制作Android应用程序时遇到了一些问题。

我的问题是,当我打开我的应用程序并加载我的网页时,然后当我想向后退一步它只是关闭应用程序。我正在尝试启用后退按钮,返回页面,但我似乎无法通过我的实际“webView”将其“绑定”

这是我的MainActivity.java

package com.kevi2129.ris.elevintra;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    @SuppressLint({ "CutPasteId", "SetJavaScriptEnabled" })
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView ourBrow = (WebView) findViewById(R.id.webView);
        ourBrow.loadUrl("http://www.ringkobingskole.dk/mobil/");
        WebView myWebView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new WebViewClient());

        }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


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

}

这是我的ActivityMain.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

感谢您的帮助。 -Kevin

2 个答案:

答案 0 :(得分:0)

覆盖onBackPressed并处理您的自定义WebView行为:

    @Override
    public void onBackPressed() {
        if(mWebView != null && mWebView.canGoBack()){
            mWebView.goBack();
        }else{
            finish();
        }
    }

答案 1 :(得分:0)

将WebView声明为类成员

 WebView myWebView; 
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView) findViewById(R.id.webView); 
        ... // rest of the code

并使用myWebView

  if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }

你也有

   WebView ourBrow = (WebView) findViewById(R.id.webView); // remove this statement
   WebView myWebView = (WebView) findViewById(R.id.webView); 

删除其中一个并使用ourBrowmyWebView

假设您删除WebView ourBrow = (WebView) findViewById(R.id.webView);首先初始化myWebView = (WebView) findViewById(R.id.webView)然后

  mywebView.loadUrl("http://www.ringkobingskole.dk/mobil/");

http://android-developers.blogspot.in/2009/12/back-and-other-hard-keys-three-stories.html

从2.0开始,我们有了一个新的小API,使这个更简单,更容易发现并且正确:

@Override
public void onBackPressed() {
if (myWebView!=null && myWebView.canGoBack())
 {
   myWebView.goBack();      
 }
else
 {
   finish();    
 }
}