ANDROID CANGOBACK无法工作,因为它应该工作

时间:2013-08-16 18:01:34

标签: android webview

我正在用webview编写一个应用程序,其中我有超链接。当我转到超链接时,我无法使用我的后退键回到我的主站点。 这是我的webview代码:

public class WebViewActivity extends Activity {

   private WebView webView;

   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);


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

    /*------------enable javascript--------------- */
    WebSettings webSettings = webView.getSettings();
    webView.getSettings().setJavaScriptEnabled(true);
    /*-------------------------------------------- */


    /*------------enable Zoom--------------- */
    webView.getSettings().setBuiltInZoomControls(true);
    /*-------------------------------------------- */
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    /*------------size of the webpage in webview--------------- */
    webView.setPadding(0, 0, 0, 0);
    webView.setInitialScale(40); // 0 - 100 where 100 is full scal
    /*-------------------------------------------- */


    /*------------call url ------------------------ */
    webView.loadUrl("http://www.example.gr");   
    webView.setWebViewClient(new MyWebViewClient() );
    /*-------------------------------------------- */


}

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        webView.loadUrl(url);
        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) && !( webView.canGoBack() ) ){
        webView.loadUrl("http://www.example.gr");
        return false;
    }

 else if (webView != null && (keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack() ) {
 webView.goBack();
return false;
}

return super.onKeyDown(keyCode, event);
}

}

0 个答案:

没有答案