首页 > 代码库 > Android网络技术之WebView常用方法

Android网络技术之WebView常用方法

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);
    }
}

 

 

?
1
2
3
4
5
<!--?xml version=1.0 encoding=utf-8?-->
<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
 
 
</linearlayout>

 

结伴旅游,一个免费的交友网站:www.jieberu.com

推推族,免费得门票,游景区:www.tuituizu.com

Android网络技术之WebView常用方法