摘自:安卓APP_ 控件(11)webView —— 简单应用:显示网页
作者:丶PURSUING
发布时间: 2021-05-11 11:50:52
网址:https://blog.csdn.net/weixin_44742824/article/details/116602469

目录

  • 简单了解
  • 使用
    • 一、配置文件manifest中添加两个权限
    • 二、添加webView控件
  • 完整源码

简单了解

Android WebView是一个特殊的View,它能用来显示网页,这个WebView类可以被用来在app中仅仅显示一张在线的网页,当然还可以用来开发浏览器。

WebView内部实现是采用渲染引擎(WebKit)来展示view的内容,提供网页前进后退、网页放大、缩小、搜索等功能。

使用

注意:在此之前,先要确保你的安卓虚拟机能够联网,因为之前做过尝试,不稳定故这里不做展开,可以拉到真机中去调试。

一、配置文件manifest中添加两个权限

联网权限

<uses-permission android:name="android.permission.INTERNET" />
  • 1

明文支持

android:usesCleartextTraffic="true"
  • 1

从Android 9.0(API级别28)开始,默认情况下禁用明文支持,因此http的url均无法在webview中加载。

如下图

二、添加webView控件

在MainActivity中添加:

        WebView webView = findViewById(R.id.webView);webView.loadUrl("http://www.baidu.com");
  • 1
  • 2

如下图:
这时候运行已经可以看到百度界面了,点一下返回按键,看到的是空白界面,实际上是加载了系统自带的浏览器。

更多细节见源码

完整源码

因为虚拟机的联网原因,放到真机中调试,故这里没有做结果的演示。就是输入网址http://www.baidu.com后点击确定即可跳转

public class MainActivity extends AppCompatActivity {WebView webView;EditText editText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);webView = findViewById(R.id.webView);//系统默认会通过手机浏览器打开网页,为了能够直接通过WebView显示网页,则必须设置webView.setWebViewClient(new WebViewClient());editText = findViewById(R.id.editText);/*设置editText的回车事件:直接闪退?editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {String string = editText.getText().toString();webView.loadUrl(string);//ENTERreturn (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);}});*/}//按键响应事件public void enterFunc(View view) {String string = editText.getText().toString();webView.loadUrl(string);}
}
  • 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

activity_main.xml
按键点击事件


设置editText的回车事件,(点击回车会闪退?),故上面用了button来代替。

   //设置editText的回车事件:直接闪退?editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {String string = editText.getText().toString();webView.loadUrl(string);//ENTERreturn (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);}});
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

安卓APP_ 控件(11)webView —— 简单应用:显示网页相关推荐

  1. 安卓APP_ 控件(10)—— ListView可上下滑动的列表(重要)与ViewHolder优化

    摘自:安卓APP_ 控件(10)-- ListView可上下滑动的列表(重要)与ViewHolder优化 作者:丶PURSUING 发布时间: 2021-04-12 23:28:27 网址:https ...

  2. 安卓APP_ 控件(9)—— PopupWindow弹窗

    摘自:安卓APP_ 控件(9)-- PopupWindow弹窗 作者:丶PURSUING 发布时间: 2021-04-05 14:41:35 网址:https://blog.csdn.net/weix ...

  3. 安卓APP_ 控件(7)——Toolbar栏目样式

    摘自:安卓APP_ 控件(7)--Toolbar栏目样式 作者:丶PURSUING 发布时间: 2021-04-02 15:42:07 网址:https://blog.csdn.net/weixin_ ...

  4. 安卓APP_ 控件(6)—— Notification通知

    摘自:安卓APP_ 控件(6)-- Notification通知 作者:丶PURSUING 发布时间: 2021-04-02 00:30:14 网址:https://blog.csdn.net/wei ...

  5. 安卓APP_ 控件(8)—— AlertDialog

    摘自:安卓APP_ 控件(8)-- AlertDialog 作者:丶PURSUING 发布时间: 2021-04-02 18:13:20 网址:https://blog.csdn.net/weixin ...

  6. 安卓APP_ 控件(5)—— ProgressBar

    摘自:安卓APP_ 控件(5)-- ProgressBar 作者:丶PURSUING 发布时间: 2021-03-31 13:03:07 网址:https://blog.csdn.net/weixin ...

  7. 安卓APP_ 控件(4)—— ImageView

    摘自:安卓APP_ 控件(4)-- ImageView 作者:丶PURSUING 发布时间: 2021-03-29 21:52:06 网址:https://blog.csdn.net/weixin_4 ...

  8. 安卓APP_ 控件(3)—— EditText

    摘自:安卓APP_ 控件(3)-- EditText 作者:丶PURSUING 发布时间: 2021-03-29 18:43:40 网址:https://blog.csdn.net/weixin_44 ...

  9. 安卓APP_ 控件(2)—— Button

    摘自:安卓APP_ 控件(2)-- Button 作者:丶PURSUING 发布时间: 2021-03-29 14:20:54 网址:https://blog.csdn.net/weixin_4474 ...

最新文章

  1. 买了云服务器和域名怎么进行解析
  2. 好玩的python代码示例-这可能是最好玩的python GUI入门实例!
  3. python 成绩分析系统_用Python编写成绩管理分析系统(故事升级版)
  4. android 移除fragment,Android Viewpager+Fragment取消预加载及Fragment方法的学习
  5. acm省赛选拔组队赛经验谈
  6. java.sql.SQLException: Protocol violation 问题解析
  7. 不了解这些“高级货”,活该你面试当炮灰。。。【石杉的架构笔记】
  8. javaio流_Java IO流
  9. 城镇化进程中的粮食生产问题
  10. 2021-04-30 AndroidStudio_3种按钮点击事件_小白龙抄作业
  11. ctypes 指针类型 byref pointer POINTER
  12. Element-UI学习之旅-Layout布局
  13. 2022年7月22日,记录我的第一篇博客
  14. 2021.11.28
  15. 【MySQL】练习二 关系数据库
  16. 选择代码覆盖工具的 10 个标准
  17. 项目管理软件有哪些,哪个好用?
  18. 多元线性回归之预测房价
  19. 安装报错:TypeError: _classify_installed_files() got an unexpected keyword argument ‘cmake_install_dir‘
  20. 如何下载MySQL各个版本

热门文章

  1. kubernetes 集群部署
  2. (五):C++分布式实时应用框架——微服务架构的演进
  3. React Native通信机制详解
  4. Android多种View动画:EasyAndroidAnimations
  5. Java设计模式12:装饰器模式
  6. Getting the right Exception Context from a Memory dump Fixed
  7. 变量定义和声明的区别~~~概念上千万不要栽跟头!!!
  8. 【python数字信号处理】——scipy库设计滤波器(IIR为例)、绘制滤波器频谱响应、IIR滤波器滤波、读写wav音频文件
  9. ubuntu100%快速安装搜狗输入法
  10. python从文件中提取特定文本_使用Python从HTML文件中提取文本