package com.rzhang.webviewtest;
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
{
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView= (WebView) findViewById(R.id.webView1);
//缩放开关
webView.getSettings().setSupportZoom(true);
//加载js
webView.getSettings().setJavaScriptEnabled(true);
//设置是否可以缩放
webView.getSettings().setBuiltInZoomControls(true);
//无限缩放
webView.getSettings().setUseWideViewPort(true);
//加载需要显示的网页
webView.loadUrl("http://www.baidu.com/");
//设置Web视图
webView.setWebViewClient(new HelloWebViewClient ());
}
@Override
//设置回退
//覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack(); //goBack()表示返回WebView的上一页面
return true;
}
return false;
}
//Web视图
private class HelloWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
@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;
}
}
简单webview的使用
最新推荐文章于 2019-01-21 11:44:06 发布