实例一:简易浏览器。
public class MainActivity extends Activity {
private EditText et_address;
private WebView webView;
private ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_address = (EditText) findViewById(R.id.et_address);
webView = (WebView) findViewById(R.id.webview);
pd = new ProgressDialog(this);
pd.setMessage("正在加载...");
//获得webview的设置
WebSettings settings = webView.getSettings();
//webview支持放大缩小
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
//自己打开网页,而非调用系统浏览器
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd.show();
}
@Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
}
});
}
public void click(View view) {
String address = et_address.getText().toString().trim();
if(TextUtils.isEmpty(address)) {
address = "http://192.168.1.100:8080";
}
webView.loadUrl(address);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
实例二: