2012-07-14 111 views
0

我試着後退按鈕添加到我的Android的WebView但我有此錯誤: 方法onBackPressed()型新WebViewClient(){}必須覆蓋或實現超方法後退按鈕在我的Android的WebView

這是我的Java文件:

package sherdle.donald.duck.app; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Window; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
public class WebPageLoader extends Activity 
{ 
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); 
    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 void onBackPressed() { 
     // do something on back. 
     return; 
     } 

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

    webView.loadUrl("http://example.com"); 
    } 
} 

感謝您的幫助。

+0

看到編輯只是複製粘貼並運行它! – Sunny 2012-07-14 19:44:52

回答

5

根據doc,在WebViewClient類中沒有onBackPressed方法。你想在你的活動上覆蓋它嗎? 嘗試smthing這樣的:

package sherdle.donald.duck.app; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Window; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
public class WebPageLoader extends Activity 
{ 
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); 
    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://example.com"); 
    } 
    @Override 
    public void onBackPressed(){ 
     if(webView.canGoBack()) webView.goBack(); 
     else super.onBackPressed(); 
    } 
} 
+0

我只想讓我的後退按鈕起作用。我不擅長android,我使用了一個模板。 – Sherdle 2012-07-14 19:28:33

+0

所以我必須將onBackPressed移動到另一個地方。在哪裏? – Sherdle 2012-07-14 19:29:00

+0

@Sherdle我編輯了我的代碼。告訴我,如果它不起作用。 – AMerle 2012-07-14 19:33:38

0

試試這個

package sherdle.donald.duck.app; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Window; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
public class WebPageLoader extends Activity 
{ 
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); 
webView = (WebView) findViewById(R.id.webview); 
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl("http://example.com"); 
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; 
    } 
}); 

    } 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    switch (keyCode) { 
    case KeyEvent.KEYCODE_BACK: 
      if(mWebView.canGoBack() == true) 

       webview.goBack(); 
       else finish(); 
       break; 

    default: 
     break; 
    } 
    return super.onKeyDown(keyCode, event); 
} 
} 

而且這種方法應該是覆蓋在Activty

0

您必須重寫活動中的onkeyDown()方法。

@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); 
} 
+0

我得到這個錯誤:Keyevent無法解析爲變量。謝謝你的幫助... – Sherdle 2012-07-14 19:36:35

0
WebView webView = (WebView) findViewById(R.id.webview); 

將其更改爲:

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

,然後用第二個答案 - 就像一個魅力!