WebView
来源:互联网 发布:linux中makefile文件 编辑:程序博客网 时间:2024/06/05 21:11
Webview,简而言之就是一个可以显示网页的控件。
优点:Webview可以作为java和javascript之间交互的桥梁,可以跨平台显示,节约开发成本,提高开发效率,能让资源利用最大化。
缺点:必须依赖于网络。
Webview其实可以用一个很简单的代码就可以实现一个网页,但是它会有bug,现在就来看一下我所总结内容,需要补充的请多多指教。
1.添加网络权限
代码就写到这里,然后我们来看一下它的效果图,让你对这个控件有个更直观的感受。
Webview其实可以用一个很简单的代码就可以实现一个网页,但是它会有bug,现在就来看一下我所总结内容,需要补充的请多多指教。
1.添加网络权限
<uses-permission android:name="android.permission.INTERNET"/>2.添加布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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="com.example.a13683.webview.MainActivity" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/edi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" android:hint="请输入要加载的网址"/> <Button android:onClick="load" android:id="@+id/but" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击加载" /> </LinearLayout> <ProgressBar android:id="@+id/progress" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:visibility="gone" style="?android:attr/progressBarStyleHorizontal"/> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> </WebView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:onClick="refurbish" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="刷新"/> <Button android:onClick="forward" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="前进"/> <Button android:onClick="back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="后退"/> </LinearLayout></LinearLayout>3.实现代码效果,有些方法没有用到,感兴趣的可以自己实践一下。
package com.example.a13683.webview;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.EditText;import android.widget.ProgressBar;import android.widget.Toast;public class MainActivity extends AppCompatActivity { private WebView webView; private EditText editText; private ProgressBar bar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);//加载布局 setContentView(R.layout.activity_main);//寻找id webView = (WebView) findViewById(R.id.webview); editText = (EditText) findViewById(R.id.edi); bar = (ProgressBar) findViewById(R.id.progress); } public void load(View view){ //获取输入框的值 String path = editText.getText().toString().trim(); //非空判断 boolean empty = TextUtils.isEmpty(path); if(empty){ Toast.makeText(MainActivity.this,"请输入网址",Toast.LENGTH_LONG).show(); return; } //只要这一行代码,打开网页,在里面会自行跳转,就不是自己的项目了,所以还有以下操作 webView.loadUrl(path);//////////////////////////这一行就可以加载一个网页 //当Webview点击时,在本App中进行操作 webView.setWebViewClient(new WebViewClient(){ /*给webview 设置一个事件监听对象重写方法 可以对网页中超链接按钮进行响应 当按下某个链接时webviewClient会调用这个方法,并传递参数 * * */ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return super.shouldOverrideUrlLoading(view, url); } }); //使触摸焦点起作用(不设置,会在点击网页文本输入框时,不能弹出软键盘及不相应其他的一些事件) webView.requestFocus(); //UI界面发生改变时进行监听 webView.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, int newProgress) { //通过代码显示进度条 bar.setVisibility(View.VISIBLE); //对进度条设置加载进度的参数 bar.setProgress(newProgress); //如果加载完毕,就隐藏进度条 if(newProgress==100){ bar.setVisibility(View.GONE); } super.onProgressChanged(view, newProgress); } }); //使用WebviewSettings对网页进行一系列初始化 webviewSettingsInit(); } private void webviewSettingsInit() { //得到一个设置对象 websettings WebSettings settings = webView.getSettings(); //让webview支持Javascript, settings.setJavaScriptEnabled(true); //使webview支持网页缩放,用这个方法必须设置支持Javascript settings.setSupportZoom(true); //设置webview只加载文字,不加载图片,节省流量,增加用户体验 settings.setBlockNetworkImage(true); } //对网页进行刷新操作 public void refurbish(View view){ //重新加载 webView.reload(); } //对网页进行前进操作 public void forward(View view){ //进行一个状态的判断 if(webView.canGoForward()){ webView.goForward(); }else { Toast.makeText(MainActivity.this,"提示:已经不能前进了",Toast.LENGTH_LONG).show(); } } //通过点击事件,对网页进行后退操作 public void back(View view){ //对状态进行判断,看是否可以后退 if(webView.canGoBack()){ webView.goBack(); } }}
代码就写到这里,然后我们来看一下它的效果图,让你对这个控件有个更直观的感受。
阅读全文
1 0
- WebView
- WebView
- WebView
- webView
- webView
- webview
- webview
- WebView
- webview
- webview
- webview
- WebView
- webView
- webview
- webView
- webview
- WebView
- WebView
- 机器学习——随机森林算法randomForest——原理及python实现
- 《统计学习方法》笔记06:LR逻辑回归模型
- 画一个增加直线或者一个减少曲线(1210)
- 将一个字符串转换为任意进制
- leetcode(32). Longest Valid Parentheses
- WebView
- 自定义的token为什么没有写入cookie中?
- char,varchar,varchar2,nvarchar(size),nvarchar2(size)
- gmp4.3.2 安装 configure: error: could not find a working compiler
- Android线程简单详细教程
- 向量范数和矩阵范数
- 视频图像处理光源篇
- 数据库 explain分析查询
- iframe嵌套的网页大小适应iframe