本文是自己学习所做笔记,欢迎转载。但请注明出处:http://blog.csdn.net/jesson20121020

有时须要在程序中浏览一些网页。当然了能够通过调用系统的浏览器来打开浏览。可是大多数情况下,这样的方式并不适用。

   以下给出怎样在程序中浏览网页。先看效果图:

事实上,这里主要是利用了WebView控件及它的一些方法。

    通过WebView的loadUrl(String url)能够装载指定的地址的网页内容,并显示在控件中,上一页和下一页的功能分别相应于WebView的goBack()和goForward()方法;

  布局文件:main.xml

<?

xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/white"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/myEditText"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
/>
<Button
android:id="@+id/bt_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转到"
android:layout_toRightOf="@id/myEditText"
android:layout_margin="10dp"
/> <WebView
android:id="@+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/black"
android:focusable="false"
android:layout_below="@id/bt_go"
android:layout_margin="10dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
>
<Button
android:id="@+id/bt_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一页"
/>
<Button
android:id="@+id/bt_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一页"
/> </LinearLayout> </RelativeLayout>

主要代码:WebViewTest

public class WebViewTest extends Activity
{ private Button go;
private EditText mEditText1;
private WebView mWebView1;
private Button bt_next,bt_previous; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); go = (Button)findViewById(R.id.bt_go);
bt_next = (Button)findViewById(R.id.bt_next);
bt_previous = (Button)findViewById(R.id.bt_previous);
go.setOnClickListener(new ClickEvent());
bt_next.setOnClickListener(new ClickEvent());
bt_previous.setOnClickListener(new ClickEvent()); mEditText1 = (EditText)findViewById(R.id.myEditText);
mEditText1.setText("http://www.baidu.com");
mWebView1 = (WebView) findViewById(R.id.myWebView); mWebView1.setWebViewClient(new WebViewClient()
{ @Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
//mEditText1.setText(url);
Toast.makeText(WebViewTest.this, "载入完成", Toast.LENGTH_SHORT).show();
} }); }
class ClickEvent implements OnClickListener{ @Override
public void onClick(View v)
{
switch(v.getId()){
case R.id.bt_go :
/*设定抓取EditText里面的内容*/
String strURI = (mEditText1.getText().toString());
/*?WebView里面显示网页数据*/
mWebView1.loadUrl(strURI);
Toast.makeText(WebViewTest.this,"正在载入"+strURI,Toast.LENGTH_LONG).show();
break;
case R.id.bt_next:
mWebView1.goForward();
//System.out.println(mWebView1.getUrl());
break;
case R.id.bt_previous:
mWebView1.goBack();
break;
} }
}
}

 

 代码中onPageFinished,能够从字面来理解就是当网页载入完成时触发,可是指网页的框架载入结束,有可能图片并未载入结束。

最后,不要忘了加訪问网络的权限:

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

至此,就能够在程序中浏览网页了。你能够在EditText输入对应的网址,从而浏览各种网页,也能够通过上一页和下一页实现网页的前进和后退功能。事实上就是一个简易版的浏览器。

Android在程序中浏览网页的更多相关文章

  1. 如何在Android应用程序中使用传感器模拟器SensorSimulator

    原文地址; 如何在Android应用程序中使用传感器模拟器 - 移动平台应用软件开发技术 - 博客频道 - CSDN.NET http://blog.csdn.net/pku_android/arti ...

  2. Android应用程序中Activity的生命周期

    Android应用程序中Activity的生命周期 对于Android来说Activity的生命周期是非常的重要,尤其是对于新学者来说,只有充分了解了Activity的生命周期,才能写出优良用户体验的 ...

  3. (Android UI)Android应用程序中资源:图片、字符串、颜色、布局等

    Android系统设计采用代码和布局分离的设计模式,因此在设计Android应用程序时需要遵循该设计模式. “把非代码资源(如图片和字符串常量)和代码分离开来始终是一种很好的做法.”---<An ...

  4. 在 Android 应用程序中使用 SQLite 数据库以及怎么用

    part one : android SQLite 简单介绍 SQLite 介绍 SQLite 一个非常流行的嵌入式数据库.它支持 SQL 语言,而且仅仅利用非常少的内存就有非常好的性能.此外它还是开 ...

  5. 在WPF程序中打开网页:使用代理服务器并可进行JS交互

    本项目环境:使用VS2010(C#)编写的WPF程序,通过CefSharp在程序的窗体中打开网页.需要能够实现网页后台JS代码中调用的方法,从网页接收数据,并能返回数据给网页.运行程序的电脑不允许上网 ...

  6. Android 在一个程序中启动另一个程序

    Android 开发有时需要在一个应用中启动另一个应用,比如Launcher加载所有的已安装的程序的列表,当点击图标时可以启动另一个应用.一般我们知道了另一个应用的包名和MainActivity的名字 ...

  7. 5.把报表集成到Web应用程序中-生成网页和导出两种方式

    转自:https://wenku.baidu.com/view/104156f9770bf78a65295462.html 第四部分,把报表集成到Web应用程序中 用MyEclipse新建一个Web ...

  8. Android 在程序中动态添加 View 布局或控件

    有时我们需要在程序中动态添加布局或控件等,下面用程序来展示一下相应的方法: 1.addView 添加View到布局容器 2.removeView 在布局容器中删掉已有的View 3.LayoutPar ...

  9. android在程序中打开另一个程序

    在开发android应用的时候,在一些情况下要有前置条件,比如这边所说的要启动时要确保别的应用程序服务已经打开  或者在操作中启动别的应用等. 先来一段google上的代码: 1. 已知包名和类名的情 ...

随机推荐

  1. 解决Unity的 The file &#39;MemoryStream&#39; is corrupted! Remove it and launch 崩溃问题

    孙广东   2015.7.30 问题:   在项目平时删除资源或者脚本资源时产生的prefab的脚本引用丢失,特别是在场景scene中丢了解决方式/// 1.又一次Clone项目/// 2.删除项目的 ...

  2. Cocos2d-x3.0 从代码中获取cocostudio编辑的UI控件

    依据名字查找控件 须要包括的头文件及名字空间: #include "cocostudio/CocoStudio.h" #include "ui/CocosGUI.h&qu ...

  3. pandas深入理解

    Pandas是一个Python库,旨在通过“标记”和“关系”数据以完成数据整理工作,库中有两个主要的数据结构Series和DataFrame In [1]: import numpy as np In ...

  4. Java-MyBatis: MyBatis3 | Java API

    ylbtech-Java-MyBatis:  MyBatis3 | Java API 1.返回顶部 1. Java API 既然你已经知道如何配置 MyBatis 和创建映射文件,你就已经准备好来提升 ...

  5. batch normalization学习理解笔记

    batch normalization学习理解笔记 最近在Andrew Ng课程中学到了Batch Normalization相关内容,通过查阅资料和原始paper,基本上弄懂了一些算法的细节部分,现 ...

  6. Python笔记(三)

    # -*- coding:utf-8 -*- # 运算符 a,b=10,20 # 算术运算符:包括+.-.*./.%.**.//运算 print "********************1 ...

  7. 关于swift构造方法

    switf 中如果遇到这样的错,,,大概错误就是,"必须要调用父类的构造方法",,可是呢,,调用了super.init()  不就是调用了构造方法了吗? 结果上去一查,,结果一名外 ...

  8. 截图 gif 图小工具

    GifCam gyazo gyazo.com Windows 10 中的内置截屏(Win+G)

  9. Java标识符规范

    1.标识符用来定义包名,类名,方法名,变量名,常量名. 2.标识符必须由字母.下划线.$符号组成,不能以数字开头.不能是Java中的保留关键字.

  10. 脚本_统计固定时间段服务器的访问量.sh

    #!bin/bash#功能:统计 1:30 到 4:30 所有访问 apache 服务器的请求有多少个#作者:liusingbon#awk 使用-F 选项指定文件内容的分隔符是/或者:#条件判断$7: ...