分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
转载请标明出处:http://blog.csdn.net/xx326664162/article/details/50902089 文章出自:薛瑄的博客
你也可以查看我的其他同类文章,也会让你有一定的收货!
- 先在要显示网页的布局文件中声明WebView
- 在Activity中实例化WebView
- 调用WebView的loadUrl( )方法,设置WevView要显示的网页
- 为了让WebView能够响应超链接功能,调用setWebViewClient( )方法,设置 WebView视图
- 在用WebView的网页打开连接后想要具有返回原来的网页的功能,可通过覆盖Activity类的onKeyDown()方法,如果不做任何处理,点击手机自带的返回按键,浏览器则会调用finish()关闭浏览器,而不是回退到上一页面
- 要访问到网络还需要在AndroidManifest.xml文件中添加权限,否则会出现Web page not available错误。
目的:实现用webview显示网页,并且回退时回到上一个页面,而不是退出WebView ,地址栏也没有。
mWebView.goBack(); //后退
mWebView.goForward();//前进
mWebView.reload(); //刷新
package com.example.netconnect3;import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.webkit.WebView;import android.webkit.WebViewClient;public class MainActivity extends Activity { private WebView webview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //2、在Activity中实例化WebView setContentView(R.layout.activity_main); webview = (WebView) findViewById(R.id.webview); //第2步也可以替换为下面这两句,就不要R.layout.activity_main布局文件了 //mwebview = new WebView(this); //实例化WebView对象 //setContentView(mwebview); // 设置WebView属性,能够执行Javascript脚本 webview.getSettings().setJavaScriptEnabled(true); //3、 加载需要显示的网页 webview.loadUrl("http://www.baidu.com/"); ///4、设置响应超链接,在安卓5.0系统,不使用下面语句超链接也是正常的,但在MIUI中安卓4.4.4中需要使用下面这条语句,才能响应超链接 webview.setWebViewClient(new HelloWebViewClient()); } @Override // 设置回退 // 5、覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法 public boolean onKeyDown(int keyCode, KeyEvent event) { //按下返回键并且webview界面可以返回 if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { webview.goBack(); // goBack()表示返回WebView的上一页面 return true; } return super.onKeyDown(keyCode,event); } // Web视图 private class HelloWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
布局文件:
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" />"</RelativeLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
还有权限:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- 1
- 2
参考:
http://blog.csdn.net/encienqi/article/details/8033182
http://www.android100.org/html/201401/18/5362.html
http://wxc.iteye.com/blog/2226465
关注我的公众号,轻松了解和学习更多技术