将代码部署到了小米手机上,url是http://baidu.com;按物理返回键,一直都没有退出整个程序,还是处于百度首页,这是为何呢?
缘分天空_0003
2014-09-27
5 回答
package com.example.webviewtest; import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.view.KeyEvent; import android.view.Menu; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends Activity { private WebView wv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wv = (WebView) findViewById(R.id.webView1); wv.loadUrl("http://baidu.com"); //设置启用javascript WebSettings settings = wv.getSettings(); settings.setJavaScriptEnabled(true); //设置启用缓存 settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //获取焦点 wv.requestFocus(); //设置使用webview自身打开网页 wv.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); //设置进度显示 wv.setWebChromeClient(new WebChromeClient() { ProgressDialog dialog; @Override public void onProgressChanged(WebView view, int newProgress) { if(newProgress==100){ closeProccgressDialog(); }else{ openProccgressDialog(); } } private void closeProccgressDialog() { if(null!=dialog){ dialog.dismiss(); dialog =null; } } private void openProccgressDialog() { if(null==dialog){ dialog = new ProgressDialog(MainActivity.this); dialog.setCanceledOnTouchOutside(false); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); dialog.setMessage("正在加载中,请稍后"); dialog.show(); } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { //按下返回键处理 if(keyCode == KeyEvent.KEYCODE_BACK&&wv.canGoBack()){ wv.goBack(); return true; } return super.onKeyDown(keyCode, event); } }
这个程序在机子上运行,启动后显示百度首页,然后按物理返回键,始终是在百度首页,要连续按很多次才会退出;将老师您的代码部署了下,也是同样的情况。请问这个可能会是什么情况导致的呢?
举报
0/150
提交
取消