进入页面后,按返回页面还是原页面。
private void init() { // TODO Auto-generated method stub webview=(WebView) findViewById(R.id.WebView); webview.loadUrl("http://www.baidu.com"); // webview.loadUrl("file:///android_assets/xxx.xml"); //覆盖WEBVIE默认通过第三方软件或系统自带浏览器打开网页 webview.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub webview.loadUrl(url); return true; } }); WebSettings settings=webview.getSettings(); settings.setJavaScriptEnabled(true); //webview 加载优先使用缓存加载 settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); webview.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, int newProgress) { // TODO Auto-generated method stub if(newProgress==100){ closeDialog(); }else{ openDialog(newProgress); } } }); } protected void openDialog(int newProgress) { // TODO Auto-generated method stub if(progressDialog==null){ progressDialog=new ProgressDialog(MainActivity.this); progressDialog.setTitle("正在加载"); progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL); progressDialog.setProgress(newProgress); progressDialog.show(); }else{ progressDialog.setProgress(newProgress); } } protected void closeDialog() { // TODO Auto-generated method stub if(progressDialog!=null&&progressDialog.isShowing()){ progressDialog.dismiss(); progressDialog=null; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(keyCode==KeyEvent.KEYCODE_BACK){ if(webview.canGoBack()){ // Toast.makeText(this, webview.getUrl(), Toast.LENGTH_SHORT).show(); webview.goBack(); return true; }else{ System.exit(0); } } return super.onKeyDown(keyCode, event); }
代码是按照视频里面写的,但是有时返回不了。不知道怎么了,求大神指点。