首页 文章 精选 留言 我的

精选列表

搜索[移动端适配],共10000篇文章
优秀的个人博客,低调大师

移动端最常用&最重要组件之一:WebView(HarmonyOS版)使用指南

现在很多APP里都内置了Web页面(比如社交APP、新闻浏览类APP),即从APP中跳转到网络Web页面或本地Web页面,这种方法便于扩展APP应用体验和更新维护内置Web页面。在HarmonyOS中,开发者可以通过WebView组件实现上述功能。 WebView组件可用于加载网页内容、获取页面详细信息和控制页面跳转,并实现APP应用与内置Web页面间的交互和通信。本期Codelab我们将用一个Demo来详细介绍WebView组件的使用,包括UI界面的创建、WebView组件创建、Web页面加载方式、APP应用与Web页面间的交互与通信四个方面内容。在开始敲代码之前,开发者们需要先下载安装Huawei DevEco Studio,可参照官网的指南进行操作: ·Huawei DevEco Studio安装指南:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/software_install-0000001053582415 接下来我们来解析本次Demo的代码结构,方便大家做一个初步的了解: ● MainAbilitySlice文件“MainAbilitySlice”文件在slice文件夹中,主要用于实现Demo逻辑,包括获取WebView对象,加载页面和实现Web页面间的通信及交互。 ● DataAbility文件“DataAbility”文件主要是通过DataAbility访问WebView加载的本地文件。 ● background_button.xml文件“background_button.xml”文件在graphic文件夹中,主要用于实现Demo中按钮的背景设置。 ● ability_main.xml文件“ability_main.xml”文件在layout文件夹中,主要实现WebView组件的创建,同时也进行诸如文本输入框和按钮等其他UI界面内容的创建。 ● text.html文件“text.html”文件在rawfile文件夹中,是新增本地Web页面的HTML文件,用于模拟WebView需要加载的本地Web页面。● config.json文件config.json文件是工程相关配置文件,这里主要用于添加WebView加载Web页面所需的网络权限申请和DataAbility的声明。 了解完基本的代码结构,我们正式进入WebView组件的使用。 UI页面的创建 我们先来创建一个UI界面(如图1 Demo的UI界面),该界面将包含: ◆TextField组件,用于接收用户输入的网址信息,本Demo中默认网址为 https://www.huawei.com/cn/; ◆WebView组件,本Demo的重点组件,我们将在“WebView组件创建”中详细描述; ◆Button组件,即按钮组件,本次Demo主界面中涉及5个按钮组件将实现以下功能:加载Web页面(Load URL)、Web网页向后预览(Go Back)、向前浏览(GO Forward)、加载本地HTML页面(Load Local HTML)和给本地网页发信息(Send Message to Local HTML)。 WebView组件创建 WebView派生于通用组件Component,包含基本组件的所有功能,可以像普通组件一样使用。也就是说,WebView组件同样可以采用XML形式和代码形式创建,本次Demo使用的是XML形式:1. 在"ability_main.xml"文件中创建WebView,示例代码如下: 2.在"slice/MainAbilitySlice.java"文件中通过如下方式获取WebView对象,示例代码如下: WebViewwebview=(WebView) findComponentById(ResourceTable.Id_webview); 当然,开发者也可以使用代码形式创建,详情请参考官网: https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-component-webview-0000001092715158 Web页面加载 WebView如何加载网络Web页面和本地Web页面呢?接下来我们将分别进行介绍。 1WebView加载网络Web页面加载网络Web页面需要申请网络权限ohos.permission.INTERNET。 我们在config.json中添加如下代码进行申请: module":{ …… "reqPermissions":[ { "name":"ohos.permission.INTERNET" } ] } 完成网络申请后,在"MainAbilitySlice.java"文件中通过webview.load(String url)方法访问具体的网络Web页面,通过WebConfig类对WebView组件进行配置,示例代码如下: WebConfigwebConfig=webview.getWebConfig(); //WebView加载URL,其中urlTextField为输入URL的TextField组件 webview.load(urlTextField.getText()); 在Web页面进行链接跳转时,WebView默认会打开目标网址,通过WebAgent对象可以捕获该行为,这里仅使用isNeedLoadUrl根据当前WebView请求检查是否加载请求,当URL以"http:"或"https:"开头,则执行加载,示例代码如下: webview.setWebAgent(newWebAgent(){ @Override publicbooleanisNeedLoadUrl(WebViewwebView,ResourceRequestrequest){ if(request==null||request.getRequestUrl()==null){ LogUtil.info(TAG,"WebAgentisNeedLoadUrl:requestisnull."); returnfalse; } Stringurl=request.getRequestUrl().toString(); if(url.startsWith("http:")||url.startsWith("https:")){ webView.load(url); returnfalse; }else{ returnsuper.isNeedLoadUrl(webView,request); } } }); 除此之外,WebAgent对象还提供了相关的回调函数以观测页面状态的变更,如onLoadingPage、onPageLoaded、onError等方法,用于页面“开始加载”、“停止加载”、“加载错误”时的调用,详情可见官网WebAgent参考。 ·官网:WebAgent参考https://developer.harmonyos.com/cn/docs/documentation/doc-references/webagent-0000001078160526 同时,WebView提供Navigator类进行历史记录的浏览和处理,开发者可通过getNavigator()方法获取该类的对象,使用canGoBack()或canGoForward()方法检查是否可以向后或向前浏览,使用goBack()或goForward()方法实现向后或向前浏览,示例代码如下: Navigatornavigator=webView.getNavigator(); if(navigator.canGoBack()){ navigator.goBack(); } if(navigator.canGoForward()){ navigator.goForward(); } 2WebView加载本地Web页面 出于安全考虑,WebView不支持直接通过File协议加载资源文件或本地文件。如开发者需实现相关业务,HarmonyOS提供两种方式:通过proce***esourceRequest方法访问文件和通过Data Ability访问文件。在本Demo中,我们将重点讲述Data Ability访问文件的形式,大家可以通过官网了解proce***esourceRequest方法的实现~·官网:加载资源文件或本地文件https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-component-webview-0000001092715158 本次Demo中,我们将自行创建一个HTML文件,放在"resources/rawfile/"目录下并将其命名为text.html。除了HTML文件常见的固定内容,我们还将在其中加入 原文链接:https://harmonyos.51cto.com/posts/4426#bkwz 想了解更多关于鸿蒙的内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区 https://harmonyos.51cto.com/#bkwz

优秀的个人博客,低调大师

Android数据适配-ExpandableListView

Android中ListView的用法基本上学的时候都会使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去实现,这次主要使用的ExpandableListView展示一种两层的效果,ExpandableListView是android中可以实现下拉list的一个控件类似于QQ那种我好友之后就是一排自己的好友,就是两层效果,实现的话使用SimpleExpandableListAdapter即可。 布局文件 先看下效果: main中xml代码: 1 2 3 4 5 6 7 8 9 10 11 <Button android:onClick= "test" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "FlyElephant" /> <ExpandableListView android:id= "@id/android:list" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:drawSelectorOnTop= "false" /> 定义一个省份的province.xml: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <TextView android:id= "@+id/list_provinceText" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:paddingBottom= "8px" android:paddingLeft= "30px" android:paddingRight= "5px" android:paddingTop= "8px" android:textSize= "20sp" /> </LinearLayout> 定义了一个地区的child.xml: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <TextView android:id= "@+id/child_text" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:paddingBottom= "8px" android:paddingLeft= "30px" android:paddingRight= "5px" android:paddingTop= "8px" android:textSize= "20sp" /> </LinearLayout> Demo实现 主要实现代码,代码中都已经注释,其中最主要的SimpleExpandableListAdapter中的参数,这个参数太多,很容易弄错,可以看下注释或者API文档: 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 // 创建一级条目 List<Map<String, String>> provinces = new ArrayList<Map<String, String>>(); //创建两个省份一级条目 Map<String, String> firstProvince= new HashMap<String, String>(); firstProvince.put( "province" , "河南" ); Map<String, String> secondProvince= new HashMap<String, String>(); secondProvince.put( "province" , "北京" ); provinces.add(firstProvince); provinces.add(secondProvince); // 创建一级条目下的的二级地区条目 List<Map<String, String>> childList1= new ArrayList<Map<String, String>>(); //同样是在一级条目目录下创建两个对应的二级条目目录 Map<String, String> child1= new HashMap<String, String>(); child1.put( "child" , "郑州" ); Map<String, String> child2 = new HashMap<String, String>(); child2.put( "child" , "开封" ); childList1.add(child1); childList1.add(child2); //同上 List<Map<String, String>> childList2 = new ArrayList<Map<String, String>>(); Map<String, String> child3 = new HashMap<String, String>(); child3.put( "child" , "海淀" ); Map<String, String> child4 = new HashMap<String, String>(); child4.put( "child" , "昌平" ); childList2.add(child3); childList2.add(child4); // 将二级条目放在一个集合里,供显示时使用 List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>(); childs.add(childList1); childs.add(childList2); /** * 使用SimpleExpandableListAdapter显示ExpandableListView * 参数1.上下文对象Context * 参数2.一级条目目录集合 * 参数3.一级条目对应的布局文件 * 参数4.fromto,就是map中的key,指定要显示的对象 * 参数5.与参数4对应,指定要显示在groups中的id * 参数6.二级条目目录集合 * 参数7.二级条目对应的布局文件 * 参数8.fromto,就是map中的key,指定要显示的对象 * 参数9.与参数8对应,指定要显示在childs中的id */ SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this , provinces, R.layout.list_group, new String[] { "province" }, new int [] { R.id.list_groupText }, childs, R.layout.child, new String[] { "child" }, new int [] { R.id.child_text }); setListAdapter(adapter); 这个mainActivity需要继承ExpandableListActivity,当然你可以设置其中的点击事件,只要重写一下方法即可: 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 /** * 设置哪个二级目录被默认选中 */ @Override public boolean setSelectedChild( int groupPosition, int childPosition, boolean shouldExpandGroup) { //do something return super .setSelectedChild(groupPosition, childPosition, shouldExpandGroup); } /** * 设置哪个一级目录被默认选中 */ @Override public void setSelectedGroup( int groupPosition) { //do something super .setSelectedGroup(groupPosition); } /** * 当二级条目被点击时响应 */ @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { //do something return super .onChildClick(parent, v, groupPosition, childPosition, id); } 效果如下: 上面这个例子写的有点单调,其实第二个你子的布局直接是空的也行,例如定义一个images.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:orientation= "horizontal" > <ImageView android:src= "@drawable/open" android:layout_width= "20dp" android:layout_height= "20dp" /> <TextView android:id= "@+id/txtName" android:paddingLeft= "10dp" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> </LinearLayout> 然后定义一个items.xml 1 2 3 4 5 6 7 8 <?xml version= "1.0" encoding= "utf-8" ?> <TextView xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/items" android:layout_width= "wrap_content" android:layout_height= "wrap_content" > </TextView> 代码调用: 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 public class MyExpandleActivity extends Activity { /** * 实现可扩展展开列ExpandableListView的三种方式 * 一是使用SimpleExpandableListAdpater将两个List集合包装成ExpandableListView 二是 * 扩展BaseExpandableListAdpter * 三是使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter */ private String[] names = { "腾讯" , "百度" , "阿里巴巴" }; private String[][] childnames = { { "马化腾" , "张小龙" , "社交" }, { "李彦宏" , "马东敏" , "搜索" }, { "马云" , "陆兆禧" , "电商" } }; private ExpandableListView ep; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_my_expandle); // 定义父列表项List数据集合 List<Map<String, String>> group = new ArrayList<Map<String, String>>(); // 定义子列表项List数据集合 List<List<Map<String, String>>> ss = new ArrayList<List<Map<String, String>>>(); for ( int i = 0 ; i < names.length; i++) { // 提供父列表的数据 Map<String, String> maps = new HashMap<String, String>(); maps.put( "names" , names[i]); group.add(maps); // 提供当前父列的子列数据 List<Map<String, String>> child = new ArrayList<Map<String, String>>(); for ( int j = 0 ; j < names.length; j++) { Map<String, String> mapsj = new HashMap<String, String>(); mapsj.put( "map" , childnames[i][j]); child.add(mapsj); } ss.add(child); } /** * 第一个参数 应用程序接口 this 第二个父列List<?extends Map<String,Object>>集合 为父列提供数据 * 第三个参数 父列显示的组件资源文件 第四个参数 键值列表 父列Map字典的key 第五个要显示的父列组件id 第六个 子列的显示资源文件 * 第七个参数 键值列表的子列Map字典的key 第八个要显示子列的组件id */ SimpleExpandableListAdapter expand = new SimpleExpandableListAdapter( this , group, R.layout.images, new String[] { "names" }, new int [] { R.id.txtName }, ss, R.layout.items, new String[] { "map" }, new int [] { R.id.items }); ep = (ExpandableListView) findViewById(R.id.expanable_mylist); ep.setAdapter(expand); } } 效果跟上面相同: 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4107356.html,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册