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

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="后退" 
            android:onClick="back"/>
        <EditText
            android:id="@+id/webview_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="http://www."
            android:singleLine="true"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="进入" 
            android:onClick="load"/>
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="刷新"
            android:onClick="reload"
            />

    </LinearLayout>
    <WebView 
        android:id="@+id/webview_wv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章