目录

  • 模块1 Android UI设计
    • ● 版本与程序
    • ● 学习目标
    • ● 本章PPT
    • ● 任务1-1 线性布局应用示例
    • ● 任务1-2 帧布局应用示例
    • ● 任务1-3 表格布局应用示例
    • ● 任务1-4 相对布局应用示例
    • ● 任务1-5 网格布局应用示例
    • ● 任务1-6 约束布局应用示例
    • ● 任务1-7 文本标签组件示例
    • ● 任务1-8 制作登陆界面
    • ● 任务1-9 进度条ProgressBar应用示例
    • ● 任务1-10 单选按钮与复选按钮
    • ● 任务1-11 简单列表示例
    • ● 任务1-12 ListActivity示例
    • ● 任务1-13 带有图片的列表(选做)
    • ● 任务1-14 没有数据的ListView(选做)
    • ● 任务1-15 改进的列表(选做)
    • ● 实战演练——生肖背后的故事
  • 模块2   Activity与多个用户界面
    • ● 学习目标
    • ● 本章PPT
    • ● 任务2-1 从Activity启动另一个Activity示例
    • ● 任务2-2 传递数据到第二个Activity示例
    • ● 实战演练-生肖的界面跳转
    • ● 任务2-3 消息提示Toast示例
    • ● 任务2-4 消息对话框示例
    • ● 任务2-5 选项菜单应用示例
    • ● 任务2-6 上下文菜单应用示例
    • ● 实战演练——BMI体质指数计算器
  • 模块3   多媒体播放与录制
    • ● 学习目标
    • ● 本章PPT
    • ● 任务3-1 音乐播放器, 播放项目资源中的音乐
    • ● 任务3-2 音乐播放器, 播放SD卡中的音乐
    • ● 任务3-3 应用MediaPlayer设计视频播放器
    • ● 任务3-4 应用VidioView设计视频播放器
    • ● 任务3-5 录音示例
    • ● 任务3-6 拍照示例
    • ● 实战演练——音乐播放器
  • 模块4   广播与服务
    • ● 学习目标
    • ● 本章PPT
    • ● 任务4-1 消息广播程序示例
    • ● 任务4-2 系统通知服务示例
    • ● 任务4-3 广播和时钟服务示例
    • ● 任务4-4 调用系统功能拨打电话
    • ● 任务4-5 后台音乐服务示例
    • ● 实战演练——播放后台音乐
  • 模块5   数据存储
    • ● 学习目标
    • ● 本章PPT
    • ● 任务5-1 内部存储文件示例
    • ● 任务5-2 SD文件示例
    • ● 任务5-3 JSON数据示例
    • ● 任务5-4 SharedPreferences示例
    • ● 任务5-5 创建与删除数据库示例
    • ● 任务5-6 数据库记录的操作示例
    • ● 实战演练——掌上日记本
  • 模块6   图像和动画
    • ● 学习目标
    • ● 本章PPT
    • ● 任务6-1 绘制几何图形示例
    • ● 任务6-2 补间动画示例
    • ● 任务6-3 属性动画示例
    • ● 任务6-4 ImageView图像浏览示例
    • ● 任务6-5 ImageSwitcher展示相册示例
    • ● 任务6-6 GridView展示相册示例
    • ● 任务6-7 游戏中触屏事件示例
    • ● 实战演练——图片与动画
  • 模块7   网络编程
    • ● 学习目标
    • ● 本章PPT
    • ● 任务7-1 应用WebView的对象浏览网页
    • ● 任务7-2 从Web服务器读取图像文件
    • ● 任务7-3 读取JSON数据
    • ● 任务7-4 解析JSON数据
    • ● 实战演练——城市天气预报
  • 模块8   分享动漫
    • ● 学习目标
    • ● 制作步骤PPT
    • ● 示范视频
    • ● 学生作品展示
  • 模块9   天气预报
    • ● 学习目标
    • ● 制作步骤PPT
    • ● 示范视频
  • 课程资源
    • ● 学生资源包
    • ● 网站地址
任务7-1 应用WebView的对象浏览网页

项目短视频


操作示范如下(时长18:18)


【例7-1】 应用WebView浏览网页

新建项目Chap07,创建布局文件activity_webview.xml及控制文件WebViewActivity.java。


布局文件activity_webview.xml的源代码

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:orientation="vertical"

    tools:context=".WebViewActivity">


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="horizontal">


        <EditText

            android:id="@+id/editText1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:ems="10"

            android:hint="http://www.baidu.com"

            android:inputType="textPersonName"

            android:text="" />


        <Button

            android:id="@+id/button1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="打开网页" />

    </LinearLayout>


    <WebView

        android:id="@+id/webView1"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

</LinearLayout>



控制文件WebViewActivity.java的源代码如下:

package com.example.chap07;


import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;

import android.view.KeyEvent;

import android.view.View;

import android.webkit.WebView;

import android.widget.Button;

import android.widget.EditText;


public class WebViewActivity extends AppCompatActivity implements View.OnClickListener {

    EditText edit;

    Button openWebBtn;

    WebView webView;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_webview);

        edit = (EditText)findViewById(R.id.editText1);

        openWebBtn= (Button)findViewById(R.id.button1);

        webView = (WebView)findViewById(R.id.webView1);

        openWebBtn.setOnClickListener(this);

    }


    @Override

    public void onClick(View view) {

        String url = edit.getText().toString();

        webView.getSettings().setJavaScriptEnabled(true);

        webView.getSettings().setAllowContentAccess(true);

        webView.getSettings().setAllowFileAccess(true);

        webView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);

        webView.loadUrl(url);

    }


    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if((keyCode==KeyEvent.KEYCODE_BACK) && webView.canGoBack()){

            webView.goBack(); //返回webView的上一页面

        }

        return true;

    }

}



打开项目配置文件AndroidManifest.xml,在application中添加一个属性,这是Android SDK升级造成。

<manifest ...>    

    <application        

        ...        

        android:usesCleartextTraffic="true"       

         ...>        

        ...    

    </application>

</manifest>

配置文件AndroidManifest.xml的源代码如下:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.chap07">


    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:usesCleartextTraffic="true"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">        

        <activity android:name=".WebViewActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>


    </application>

    <uses-permission android:name="android.permission.INTERNET" />

</manifest>