浅入浅出Android(016):分别使用WebView和Intent访问网页

本文介绍如何在Android应用中使用WebView组件加载并显示网页内容。通过创建项目、添加联网权限、修改布局文件及活动代码,实现了两种方式访问百度网站的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android studio 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.youkuaiyun.com/jueblog/article/details/12985045

转载于:https://my.oschina.net/letiantian/blog/323297

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值