androidstudio 0.8.6;android虚拟机版本是4.*。

WebView组件用来访问网页,也可以渲染给出的html代码,它有很多选项。

1、建立项目MyApplication


2、增加联网权限

在AndroidManifest.xml添加<uses-permission android:name="android.permission.INTERNET" />,该文件最终内容是:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.letian.myapplication" >    <uses-permission android:name="android.permission.INTERNET" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MyActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>


3、修改主布局文件layout_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MyActivity">    <Button        android:id="@+id/button01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="进入百度(webview)" />    <Button        android:id="@+id/button02"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/button01"        android:text="进入百度(intent)" />    <ScrollView        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_below="@+id/button02">        <WebView            android:id="@+id/webview01"            android:layout_width="fill_parent"            android:layout_height="fill_parent" />    </ScrollView></RelativeLayout>


4、修改MyActivity.java

package com.example.letian.myapplication;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.webkit.WebView;import android.widget.Button;public class MyActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_my);        final Button btn01 = (Button) this.findViewById(R.id.button01);        final Button btn02 = (Button) this.findViewById(R.id.button02);        final WebView wv01 = (WebView) this.findViewById(R.id.webview01);        wv01.getSettings().setJavaScriptEnabled(true);  // 启用js        final String url = "http://www.baidu.com";        btn01.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                wv01.loadUrl(url);//                wv01.loadData("<h1>hello</h1>","text/html","utf-8");            }        });        btn02.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Uri uri = Uri.parse(url);                Intent intent = new Intent(Intent.ACTION_VIEW, uri);                startActivity(intent);            }        });    }}



5、效果图




点击“进入百度(WebView)”按钮:



点击“进入百度(intent)”按钮:



需要按左下角的返回键返回到原先的界面。

参考:

Android编程典型实例与项目开发吴亚峰编著

http://blog.csdn.net/jueblog/article/details/12985045

更多相关文章

  1. android中正确导入第三方jar包
  2. Android类说明---Scroller
  3. Android(安卓)事件分发机制总结篇
  4. C虾仔笔记 - CheckBox多选按钮
  5. 百度地图长按事件
  6. 破解android签名验证
  7. android的事件分发
  8. 将获取的html源代码格式化输出
  9. list滑动删除item

随机推荐

  1. Android(安卓)studio出现Error:Plugin is
  2. 【Android(安卓)应用开发】Ubuntu 下 And
  3. Android中onCreateOptionsMenu(Menu menu
  4. Android
  5. [置顶] 使用BleLib的轻松搞定Android低功
  6. 【Android应用开发】-(19)Android(安卓)串
  7. android自动化测试工具简介
  8. 2013.03.19(8)———android 打开url 浏览
  9. Android(安卓)GridView的使用
  10. android:launchMode — “standard” “s