Android手机中内置了一款高性能webkit内核浏览器,在SDK中封装成了WebView组件,它可以用来浏览网络内容。那么,如何使用它呢?
***次使用WebView控件加载组件
1)mainfest.xml添加internet权限
<manifest>
<uses-permission android:name="android.permission.INTERNET" />
manifest>
- 1.
- 2.
- 3.
2)layout添加webView组件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
LinearLayout>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
3)添加activity
package com.example.webview1;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends Activity {
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
webView = (WebView) findViewById(R.id.webview);
//设置javasctipt可用
webView.getSettings().setJavaScriptEnabled(true);
//加载url,但是不会显示,注意哦!!
webView.loadUrl("http://www.baidu.com");
//指定显示控件(class)
webView.setWebViewClient(new myWebViewClient());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_web_view, menu);
return true;
}
//为了让回退键管用
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==event.KEYCODE_BACK&&webView.canGoBack()){
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* webView视图客户端
* @author Administrator
*
*/
class myWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
// return super.shouldOverrideUrlLoading(view, url);
webView.loadUrl(url);
return true;
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
4)ok!大功告成,运行吧!!
5)模拟器***用bluestacks
6)至于其它功能还是多看看其它的文章吧。