package com.example.webview1;
import android.os.Bundle; import android.app.Activity; import android.view.KeyEvent; import android.view.Menu; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast;
public class MainActivity extends Activity { private WebView web; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); }
private void init() { // TODO Auto-generated method stub web=(WebView) findViewById(R.id.webView1); WebSettings webset = web.getSettings();//打开javascript webset.setJavaScriptEnabled(true); web.setWebViewClient(new WebViewClient()//不使用默认浏览器 { public boolean shouldOverrideUrlLoading(WebView view,String url) { view.loadUrl(url); return true; } }); web.setWebChromeClient(new WebChromeClient() {
/* (non-Javadoc) * @see android.webkit.WebChromeClient#onProgressChanged(android.webkit.WebView, int) */ @Override public void onProgressChanged(WebView view, int newProgress) { // TODO Auto-generated method stub 网页加载进度 if(newProgress==100) { closeDialog(); } else { openDialog(newProgress); } super.onProgressChanged(view, newProgress); } }); web.loadUrl("http://www.baidu.com"); }
protected void closeDialog() { // TODO Auto-generated method stub }
protected void openDialog(int newProgress) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, String.valueOf(newProgress), Toast.LENGTH_SHORT).show(); }
@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; }
/* (non-Javadoc) * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent) */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) {//返回网页, // TODO Auto-generated method stub if(keyCode==KeyEvent.KEYCODE_BACK) { if(web.canGoBack()) { web.goBack(); return true; } else { System.exit(0); } } return super.onKeyDown(keyCode, event); }
}