Android WebView的前进、后退、与刷新
2016-03-16 09:23
513 查看
http://blog.csdn.net/encienqi/article/details/8033182
http://www.android100.org/html/201401/18/5362.html
目的:实现用webview显示网页,并且回退时回到上一个页面,而不是退出WebView ,地址栏也没有。
mWebView.goBack(); //后退
mWebView.goForward();//前进
mWebView.reload(); //刷新
布局文件:
还有权限:
http://www.android100.org/html/201401/18/5362.html
目的:实现用webview显示网页,并且回退时回到上一个页面,而不是退出WebView ,地址栏也没有。
mWebView.goBack(); //后退
mWebView.goForward();//前进
mWebView.reload(); //刷新
[code]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); setContentView(R.layout.activity_main); webview = (WebView) findViewById(R.id.webview); // 设置WebView属性,能够执行Javascript脚本 webview.getSettings().setJavaScriptEnabled(true); // 加载需要显示的网页 webview.loadUrl("http://www.baidu.com/"); // 设置Web视图 webview.setWebViewClient(new HelloWebViewClient()); } @Override // 设置回退 // 覆盖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; } } }
布局文件:
[code] <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>
还有权限:
[code]<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
相关文章推荐
- JS 控制VIEWPORT在微信上正常显示
- Android 使用NineOldAndroids实现绚丽的ListView左右滑动删除Item效果
- iOS NSObject
- 在写一个iOS应用之前必须做的7件事(附相关资源)
- Android xUtils框架
- Android Studio:xxx is not an enclosing class 错误的解决方法
- popwindow下拉菜单 显示位置问题 类似微信的菜单栏
- Android时间轴的实现
- Android中的Zip解压缩
- 详解在iOS App中自定义和隐藏状态栏的方法
- Android聊天软件界面开发
- Android Data Binding -解放你的代码-处女座福音
- Android View 如何测量
- 羊皮书APP(Android版)开发系列(十一)客户端与服务器端时间校准
- getApplicationContext()、Activity.this、 getBaseContext区别
- Android studio用的几个插件
- python函数 callable(object)
- Android知识点大全(一)
- android gravity和layout_gravity区别
- 36个Android开发常用代码片段