WebView
allows you to create your own window for viewing web pages (or even develop a complete
browser). In this tutorial, you'll create a simpleActivity
that can view and navigate web
pages.
-
Create a new project namedHelloWebView.
-
Open the
res/layout/main.xml
file 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"
/>
-
Now open the
HelloWebView.java
file. At the top of the class, declare aWebView
object:
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 memberWebView
with the one from theActivity
layout;
requests aWebSettings
object withgetSettings()
;
and enables JavaScript for theWebView
withsetJavaScriptEnabled(boolean)
.
Finally, an initial web page is loaded withloadUrl(String)
.
-
Because this application needs access to the Internet, you need to add the appropriate permissions to the Android manifest file. Open the
AndroidManifest.xml
file and add the following as a child of the<manifest>
element:
<uses-permission android:name="android.permission.INTERNET" />
-
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">
-
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 thisActivity
isn't
technically enabled to do so. Instead of adding an intent filter to view web pages, you can override theWebViewClient
class
and enable thisActivity
to handle its own URL requests.
-
In the
HelloAndroid
Activity, add this nested class:
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
-
Then towards the end of the
onCreate(Bundle)
method, set an instance
of theHelloWebViewClient
as theWebViewClient
:
mWebView.setWebViewClient(new HelloWebViewClient());
This line can go anywhere following the initialization of theWebView
object.
This creates aWebViewClient
that will load any URL selected from thisWebView
into
the sameWebView
. TheshouldOverrideUrlLoading(WebView,
String)
method is passed the currentWebView
and the URL requested, so all it
needs to do is load the URL in the given view. Returningtrue
says 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.
-
To handle the BACK button key press, add the following method inside the
HelloWebView
Activity:
@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 theKeyEvent
to
check whether the key pressed is the BACK button and whether theWebView
is 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 theWebView
history.Returningtrue
indicates
that the event has been handled. If this condition is not met, then the event is sent back to the system.
-
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 进度条 标题 刷新 分享 适合初学者 代码有注释
Android开发之常用框架WebView详解代码。超详细,送给初学者,完全掌握此控件,博文介绍:http://blog.csdn.net/qq_21376985/article/details/51703282
webview最基本的用法,简单易懂,适合初学者
用WebView实现的访问网页的程序,比较简单,适用于初学者。
Android开发,使用Kotlin实现WebView。只是一个小demo,离实际的应用还有很远,可以给初学者做一些参考。
适用于Android WebView初学者, 讲解了加载网页的两种方式以及其三个辅助类的用法
一个android平台上的webview demo,供初学者参考学习,提供了webview各种属性设置
android利用webview实现企业网站移动宣传,适合初学安卓的人
C#、WPF使用技巧,实战应用开发小系统参考资料,源码参考。经测试可运行。 详细介绍了一些WPF框架的各种功能和模块,以及如何使用WPF进行GUI...适用于初学者和有经验的开发者,能够帮助你快速上手WPF并掌握其高级特性。
初学者必看 1、LinearLayout Button、RadioGroup、 CheckBox 2、TableLayout 3、FrameLayout 霓虹灯效果 4、RelativeLayout 梅花效果 5、自定义view跟着触点走的小球 6、 ListView 列表视图 7、WebView web视图 8、...
微信小程序地图导航,微信小程序地图导航,对初学者非常有用
适合android初学者学习WebView开发的样例
我的小作品,简单的车标的小程序,可以为初学者提供参考。
Iphone在应用APP中打开第三方应用APP,适合初学者,学习打开其他第三方应用,主要是info.plist中的URL types节点下的URL Scheme下的item0 自定义的值,这个应用我定义的chenshone,另外一个定义的chenshtwo,所以...
Android进程间的通信之AIDL简单实例,主要是一下流程,希望大神们勿喷,也希望对初学者有所帮助
android做了2个窗体,连接javawebservice,web连接sql2005数据库,实现了android的登录用户名密码验证,初学者写的,代码有点乱,望见谅
reactNative初学者demo 介绍 根据reactNative零基础教程编写的实例 教程中是用ES5写的ios版 我是用ES6写的android版 安装与运行 npm install react-native run-android 组件 Text, View, Image,TabNavigator,...
作为初学者很多细节点都破费周折,分享下,大家少走点弯路吧。不过还是建议大家自己收敲一遍。对照下面的URL,里面有个数据转化的代码是作为教学用的,实际工作中可以优化。 代码实际包含的点大致描述下: 1。 基础...
初学者各种UI控件大全,对于学习UI非常有帮助。里边包括button,label,switch,webView......一系列ui控件。
使用了有道翻译的Api以及GankIo的Api,非常感谢开源人对初学者的帮助! 应用截图 简单介绍 1、网络层采用Retrofit加RxJava的模式,访问的接口在这里-->http://gank.io/api。 2、图片加载使用的是Glide...