Webview后退按钮

时间:2013-10-27 07:37:08

标签: javascript android webview

想知道如何让webview回归。

尝试重新按下webview,但似乎用我的代码它不起作用。不确定我是否发起了它,仍然对此感到困惑。

还可以在webview中删除滚动条吗?我发现了一些关于它的帖子,但我仍然想知道如何将它放入我的代码中

package com.webapp.area956;

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

public class MainActivity extends Activity {





@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebViewClient yourWebClient = new WebViewClient()

        {
           // Override page so it's load on my view only
           @Override
           public boolean shouldOverrideUrlLoading(WebView  view, String  url)
           {
            // This line we let me load only pages inside Firstdroid Webpage
            if ( url.contains("area956") == true )
               // Load new URL Don't override URL Link
               return false;

            // Return true to override url loading (In this case do nothing).
            return true;
           }
       };

    String url = "http://www.area956.com";
    WebView view = (WebView) this.findViewById(R.id.webView1);
    view.setWebViewClient(yourWebClient);
    view.getSettings().setJavaScriptEnabled(true);
    view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    view.loadUrl(url);

}
public void onBackPressed (){

    if (WebView.canGoBack()) {
            WebView.goBack();       
    }
    else {
            super.onBackPressed();
            finish();
    }
}

@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;
}

}

1 个答案:

答案 0 :(得分:0)

将webview作为班级成员

WebView view;
@Override
public void onCreate(Bundle savedInstanceState) {

然后初始化

view = (WebView) this.findViewById(R.id.webView1);
view.setWebViewClient(yourWebClient);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
view.loadUrl(url);

然后使用onKeyDown

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

您已在WebView view中声明了onCreate并在那里进行了初始化。因此它变为onCreate的本地。

编辑:

从2.0开始,您也可以使用onBackPressed

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

  public void onBackPressed (){
    super.onBackPressed();
    if (view.canGoBack()) {
            view.goBack();       
    }
    else {

            finish();
    }
}