作者:IHH_MCWONG_142 | 来源:互联网 | 2023-06-25 16:47
publicclassWebViewTestextendsActivity{privateWebViewwv;privateEditTextet;@Overrid
public class WebViewTest extends Activity {
private WebView wv;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
wv = (WebView) findViewById(R.id.webview_wv);
wv.loadUrl("http://www.baidu.com");
initWV();
et = (EditText) findViewById(R.id.webview_et);
et.setSelection(et.getText().length());
}
private void initWV() {
// TODO Auto-generated method stub
//设置内核,支持ajax
wv.setWebChromeClient(new WebChromeClient());
//new WebViewClient()默认基于webkit内核
wv.setWebViewClient(new WebViewClient(){
//从一个网页跳到另一个网页的方式,return true则为目标页面在当前webView打开,不会调用系统默认浏览器
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return true;
}
//当页面加载完毕调用的方法
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
Toast.makeText(WebViewTest.this, "加载完毕!", 0).show();
super.onPageFinished(view, url);
}
//页面开始加载时调用的方法
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
et.setText(url);
super.onPageStarted(view, url, favicon);
}
});
//设置支持js脚本
wv.getSettings().setJavascriptEnabled(true);
//设置支持手指放大
wv.getSettings().setSupportZoom(true);
wv.getSettings().setBuiltInZoomControls(true);
}
public void load(View view){
//加载URL
wv.loadUrl(et.getText().toString().trim());
}
public void reload(View view){
//重新载入,刷新页面
wv.reload();
}
public void back(View view){
//后退
wv.goBack();
}
//设置(拦截)手机上的按键触摸时间
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
wv.goBack();
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
}
xml代码:
Android网络技术之WebView常用方法