`
yidongkaifa
  • 浏览: 4069894 次
文章分类
社区版块
存档分类
最新评论

初学 WebView

 
阅读更多

WebViewallows you to create your own window for viewing web pages (or even develop a complete browser). In this tutorial, you'll create a simpleActivitythat can view and navigate web pages.


  1. Create a new project namedHelloWebView.
  2. Open theres/layout/main.xmlfile and insert the following:
    <?xml version="1.0" encoding="utf-8"?>
    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
  3. Now open theHelloWebView.javafile. At the top of the class, declare aWebViewobject:
    WebView mWebView;

    Then use the following code for theonCreate()method:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.baidu.com");
    }

    This initializes the memberWebViewwith the one from theActivitylayout; requests aWebSettingsobject withgetSettings(); and enables JavaScript for theWebViewwithsetJavaScriptEnabled(boolean). Finally, an initial web page is loaded withloadUrl(String).

  4. Because this application needs access to the Internet, you need to add the appropriate permissions to the Android manifest file. Open theAndroidManifest.xmlfile and add the following as a child of the<manifest>element:
    <uses-permission android:name="android.permission.INTERNET" />
  5. While you're in the manifest, give some more space for web pages by removing the title bar, with the "NoTitleBar" theme:
    <activity android:name="你自己的activity name" android:label="@string/app_name"
      android:theme="@android:style/Theme.NoTitleBar">
  6. Now run the application.

    You now have a simplest web page viewer. It's not quite a browser yet because as soon as you click a link, the default Android Browser handles the Intent to view a web page, because thisActivityisn't technically enabled to do so. Instead of adding an intent filter to view web pages, you can override theWebViewClientclass and enable thisActivityto handle its own URL requests.

  7. In theHelloAndroidActivity, add this nested class:
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
  8. Then towards the end of theonCreate(Bundle)method, set an instance of theHelloWebViewClientas theWebViewClient:
    mWebView.setWebViewClient(new HelloWebViewClient());

    This line can go anywhere following the initialization of theWebViewobject.

    This creates aWebViewClientthat will load any URL selected from thisWebViewinto the sameWebView. TheshouldOverrideUrlLoading(WebView, String)method is passed the currentWebViewand the URL requested, so all it needs to do is load the URL in the given view. Returningtruesays that the method has handled the URL and the event should not propagate (in which case, an Intent would be created that's handled by the Browser application).

    If you run the application again, new pages will now load in this Activity. However, you can't navigate back to previous pages. To do this, you need to handle the BACK button on the device, so that it will return to the previous page, rather than exit the application.

  9. To handle the BACK button key press, add the following method inside theHelloWebViewActivity:
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    ThisonKeyDown(int, KeyEvent)callback method will be called anytime a button is pressed while in the Activity. The condition inside uses theKeyEventto check whether the key pressed is the BACK button and whether theWebViewis actually capable of navigating back (if it has a history). If both are true, then thegoBack()method is called, which will navigate back one step in theWebViewhistory.Returningtrueindicates that the event has been handled. If this condition is not met, then the event is sent back to the system.

  10. Run the application again. You'll now be able to follow links and navigate back through the page history.

When you open the application, it should look like this:


分享到:
评论

相关推荐

    WebView 实例(适合初学者,有注释)

    WebView 进度条 标题 刷新 分享 适合初学者 代码有注释

    Android开发之常用框架WebView详解代码。超详细,送给初学者,完全掌握此控件

    Android开发之常用框架WebView详解代码。超详细,送给初学者,完全掌握此控件,博文介绍:http://blog.csdn.net/qq_21376985/article/details/51703282

    webview的简单样例

    webview最基本的用法,简单易懂,适合初学者

    AndroidWebView

    用WebView实现的访问网页的程序,比较简单,适用于初学者。

    TestWebView.zip

    Android开发,使用Kotlin实现WebView。只是一个小demo,离实际的应用还有很远,可以给初学者做一些参考。

    WebView笔记

    适用于Android WebView初学者, 讲解了加载网页的两种方式以及其三个辅助类的用法

    webview.zip

    一个android平台上的webview demo,供初学者参考学习,提供了webview各种属性设置

    android利用webview实现企业网站移动宣传

    android利用webview实现企业网站移动宣传,适合初学安卓的人

    在wpf应用程序中,通过WebView2加载并显示Cesium三维地球.zip

    C#、WPF使用技巧,实战应用开发小系统参考资料,源码参考。经测试可运行。 详细介绍了一些WPF框架的各种功能和模块,以及如何使用WPF进行GUI...适用于初学者和有经验的开发者,能够帮助你快速上手WPF并掌握其高级特性。

    android初学者入门项目

    初学者必看 1、LinearLayout Button、RadioGroup、 CheckBox 2、TableLayout 3、FrameLayout 霓虹灯效果 4、RelativeLayout 梅花效果 5、自定义view跟着触点走的小球 6、 ListView 列表视图 7、WebView web视图 8、...

    微信小程序地图导航,对初学者非常有用

    微信小程序地图导航,微信小程序地图导航,对初学者非常有用

    简单浏览器

    适合android初学者学习WebView开发的样例

    车标(供初学者)

    我的小作品,简单的车标的小程序,可以为初学者提供参考。

    Iphone在应用APP中打开第三方应用APP

    Iphone在应用APP中打开第三方应用APP,适合初学者,学习打开其他第三方应用,主要是info.plist中的URL types节点下的URL Scheme下的item0 自定义的值,这个应用我定义的chenshone,另外一个定义的chenshtwo,所以...

    Android进程间的通信AIDL实例

    Android进程间的通信之AIDL简单实例,主要是一下流程,希望大神们勿喷,也希望对初学者有所帮助

    android javaweb登录

    android做了2个窗体,连接javawebservice,web连接sql2005数据库,实现了android的登录用户名密码验证,初学者写的,代码有点乱,望见谅

    reactNativeDemo:reactNative初学者demo

    reactNative初学者demo 介绍 根据reactNative零基础教程编写的实例 教程中是用ES5写的ios版 我是用ES6写的android版 安装与运行 npm install react-native run-android 组件 Text, View, Image,TabNavigator,...

    使用C++创建你的第一个Metro风格应用

    作为初学者很多细节点都破费周折,分享下,大家少走点弯路吧。不过还是建议大家自己收敲一遍。对照下面的URL,里面有个数据转化的代码是作为教学用的,实际工作中可以优化。 代码实际包含的点大致描述下: 1。 基础...

    iOS 各种UI控件大全

    初学者各种UI控件大全,对于学习UI非常有帮助。里边包括button,label,switch,webView......一系列ui控件。

    Android代码-gankiodemo

    使用了有道翻译的Api以及GankIo的Api,非常感谢开源人对初学者的帮助! 应用截图 简单介绍 1、网络层采用Retrofit加RxJava的模式,访问的接口在这里--&gt;http://gank.io/api。 2、图片加载使用的是Glide...

Global site tag (gtag.js) - Google Analytics