【HarmonyOS Next之旅】兼容JS的类Web开发 -> 常见组件(一)
zhezhongyun 2025-06-09 07:20 3 浏览
目录
1 -> List
1.1 -> 创建List组件
1.2 -> 添加滚动条
1.3 -> 添加侧边索引栏
1.4 -> 实现列表折叠和展开
1.5 -> 场景示例
2 -> dialog
2.1 -> 创建Dialog组件
2.2 -> 设置弹窗响应
2.3 -> 场景示例
3 -> form
3.1 -> 创建Form组件
3.2 -> 实现表单缩放
3.3 -> 设置Form样式
3.4 -> 添加响应事件
3.5 -> 场景示例
编辑
1 -> List
List是用来显示列表的组件,包含一系列相同宽度的列表项,适合连续、多行地呈现同类数据。
1.1 -> 创建List组件
在pages/index目录下的hml文件中创建一个List组件。
<!-- index.hml -->
<div class="container">
<list>
<list-item class="listItem"></list-item>
<list-item class="listItem"></list-item>
<list-item class="listItem"></list-item>
<list-item class="listItem"></list-item>
</list>
</div>
/* test.css */
.container {
width:100%;
height:100%;
flex-direction: column;
align-items: center;
background-color: #F1F3F5;
}
.listItem{
height: 20%;
background-color:#d2e0e0;
margin-top: 20px;
}
编辑
说明
- <list-item-group>是<list>的子组件,实现列表分组功能,不能再嵌套<list>,可以嵌套<list-item>。
- <list-item>是<list>的子组件,展示列表的具体项。
1.2 -> 添加滚动条
设置scrollbar属性为on即可在屏幕右侧生成滚动条,实现长列表或者屏幕滚动等效果。
<!-- index.hml -->
<div class="container">
<list class="listCss" scrollbar="on" >
<list-item class="listItem"></list-item>
<list-item class="listItem"></list-item>
<list-item class="listItem"></list-item>
<list-item class="listItem"></list-item>
<list-item class="listItem"></list-item>
<list-item class="listItem"></list-item>
</list>
</div>
/* index.css */
.container {
flex-direction: column;
background-color: #F1F3F5;
}
.listItem{
height: 20%;
background-color:#d2e0e0;
margin-top: 20px;
}
.listCss{
height: 100%;
scrollbar-color: #8e8b8b;
scrollbar-width: 50px;
}
编辑
1.3 -> 添加侧边索引栏
设置indexer属性为自定义索引时,索引栏会显示在列表右边界处,indexer属性设置为true,默认为字母索引表。
<!-- index.hml -->
<div class="container">
<list class="listCss" indexer="{{['#','1','2','3','4','5','6','7','8']}}" >
<list-item class="listItem" section="#" ></list-item>
</list>
</div>
/* index.css */
.container{
flex-direction: column;
background-color: #F1F3F5;
}
.listCss{
height: 100%;
flex-direction: column;
columns: 1
}
编辑
说明
- indexer属性生效需要flex-direction属性配合设置为column,且columns属性设置为1。
- indexer可以自定义索引表,自定义时"#"必须要存在。
1.4 -> 实现列表折叠和展开
为List组件添加groupcollapse和groupexpand事件实现列表的折叠和展开。
<!-- index.hml -->
<div class="doc-page">
<list style="width: 100%;" id="mylist">
<list-item-group for="listgroup in list" id="{{listgroup.value}}" ongroupcollapse="collapse" ongroupexpand="expand">
<list-item type="item" style="background-color:#FFF0F5;height:95px;">
<div class="item-group-child">
<text>One---{{listgroup.value}}</text>
</div>
</list-item>
<list-item type="item" style="background-color: #87CEFA;height:145px;" primary="true">
<div class="item-group-child">
<text>Primary---{{listgroup.value}}</text>
</div>
</list-item>
</list-item-group>
</list>
</div>
/* index.css */
.doc-page {
flex-direction: column;
background-color: #F1F3F5;
}
list-item{
margin-top:30px;
}
.top-list-item {
width:100%;
background-color:#D4F2E7;
}
.item-group-child {
justify-content: center;
align-items: center;
width:100%;
}
// test.js
import prompt from '@system.prompt';
export default {
data: {
direction: 'column',
list: []
},
onInit() {
this.list = []
this.listAdd = []
for (var i = 1; i <= 2; i++) {
var dataItem = {
value: 'GROUP' + i,
};
this.list.push(dataItem);
}
},
collapse(e) {
prompt.showToast({
message: 'Close ' + e.groupid
})
},
expand(e) {
prompt.showToast({
message: 'Open ' + e.groupid
})
}
}
编辑
说明
- groupcollapse和groupexpand事件仅支持list-item-group组件使用。
1.5 -> 场景示例
在本场景中,可以根据字母索引表查找对应联系人。
<!-- index.hml -->
<div class="doc-page">
<text style="font-size: 35px; font-weight: 500; text-align: center; margin-top: 20px; margin-bottom: 20px;">
<span>Contacts</span>
</text>
<list class="list" indexer="true">
<list-item class="item" for="{{namelist}}" type="{{$item.section}}" section="{{$item.section}}">
<div class="container">
<div class="in-container">
<text class="name">{{$item.name}}</text>
<text class="number">18888888888</text>
</div>
</div>
</list-item>
<list-item type="end" class="item">
<div style="align-items:center;justify-content:center;width:750px;">
<text style="text-align: center;">Total: 10</text>
</div>
</list-item>
</list>
</div>
/* index.css */
.doc-page {
width: 100%;
height: 100%;
flex-direction: column;
background-color: #F1F3F5;
}
.list {
width: 100%;
height: 90%;
flex-grow: 1;
}
.item {
height: 120px;
padding-left: 10%;
border-top: 1px solid #dcdcdc;
}
.name {
color: #000000;
font-size: 39px;
}
.number {
color: black;
font-size: 25px;
}
.container {
flex-direction: row;
align-items: center;
}
.in-container {
flex-direction: column;
justify-content: space-around;
}
// test.js
export default {
data: {
namelist:[{
name: 'Zoey',
section:'Z'
},{
name: 'Quin',
section:'Q'
},{
name:'Sam',
section:'S'
},{
name:'Leo',
section:'L'
},{
name:'Zach',
section:'Z'
},{
name:'Wade',
section:'W'
},{
name:'Zoe',
section:'Z'
},{
name:'Warren',
section:'W'
},{
name:'Kyle',
section:'K'
},{
name:'Zaneta',
section:'Z'
}]
},
onInit() {
}
}
编辑
2 -> dialog
Dialog组件用于创建自定义弹窗,通常用来展示用户当前需要或用户必须关注的信息或操作。
2.1 -> 创建Dialog组件
在pages/index目录下的hml文件中创建一个Dialog组件,并添加Button组件来触发Dialog。Dialog组件仅支持width、height、margin、margin-[left|top|right|bottom]、margin-[start|end]样式。
<!-- test.hml -->
<div class="doc-page">
<dialog class="dialogClass" id="dialogId" dragable="true">
<div class="content">
<text>this is a dialog</text>
</div>
</dialog>
<button value="click me" onclick="openDialog"></button>
</div>
/* test.css */
.doc-page {
width:100%;
height:100%;
flex-direction: column;
align-items: center;
justify-content: center;
background-color: #F1F3F5;
}
.dialogClass{
width: 80%;
height: 250px;
margin-start: 1%;
}
.content{
width: 100%;
height: 250px;
justify-content: center;
background-color: #e8ebec;
border-radius: 20px;
}
text{
width: 100%;
height: 100%;
text-align: center;
}
button{
width: 70%;
height: 60px;
}
/* test.js */
export default {
//Touch to open the dialog box.
openDialog(){
this.$element('dialogId').show()
},
}
编辑
2.2 -> 设置弹窗响应
点击页面上非Dialog的区域时,将触发cancel事件而关闭弹窗。同时也可以通过对Dialog添加show和close方法来显示和关闭弹窗。
<!-- test.hml -->
<div class="doc-page">
<dialog class="dialogClass" id="dialogId" oncancel="canceldialog">
<div class="dialogDiv">
<text>dialog</text>
<button value="confirm" onclick="confirmClick"></button>
</div>
</dialog>
<button value="click me" onclick="openDialog"></button>
</div>
/* test.css */
.doc-page {
width:100%;
height:100%;
flex-direction: column;
align-items: center;
justify-content: center;
background-color: #F1F3F5;
}
.dialogClass{
width: 80%;
height: 300px;
margin-start: 1%;
}
.dialogDiv{
width: 100%;
flex-direction: column;
justify-content: center;
align-self: center;
}
text{
height: 100px;
align-self: center;
}
button{
align-self: center;
margin-top: 20px;
width: 60%;
height: 80px;
}
/* test.js */
import prompt from '@system.prompt';
export default {
canceldialog(e){
prompt.showToast({
message: 'dialogCancel'
})
},
openDialog(){
this.$element('dialogId').show()
prompt.showToast({
message: 'dialogShow'
})
},
confirmClick(e) {
this.$element('dialogId').close()
prompt.showToast({
message: 'dialogClose'
})
},
}
编辑
说明
- 仅支持单个子组件。
- Dialog属性、样式均不支持动态更新。
- Dialog组件不支持focusable、click-effect属性。
2.3 -> 场景示例
在本场景中,可以通过Dialog组件实现一个日程表。弹窗在打开状态下,利用Textarea组件输入当前日程,点击确认按钮后获取当前时间并保存输入文本。最后以列表形式将各日程进行展示。
<!-- test.hml -->
<div class="doc-page">
<text style="margin-top: 60px;margin-left: 30px;">
<span>{{date}} events</span>
</text>
<div class="btndiv">
<button type="circle" class="btn" onclick="addschedule">+</button>
</div>
<!-- for Render events data -->
<list style="width: 100%;">
<list-item type="item" for="schedulelist" style="width:100%;height: 200px;">
<div class="schedulediv">
<text class="text1">{{date}} event</text>
<text class="text2">{{$item.schedule}}</text>
</div>
</list-item>
</list>
<dialog id="datedialog" oncancel="canceldialog" >
<div class="dialogdiv">
<div class="innertxt">
<text class="text3">{{date}}</text>
<text class="text4">New event</text>
</div>
<textarea placeholder="Event information" onchange="getschedule" class="area" extend="true"></textarea>
<div class="innerbtn">
<button type="text" value="Cancel" onclick="cancelschedule" class="btntxt"></button>
<button type="text" value="OK" onclick="setschedule" class="btntxt"></button>
</div>
</div>
</dialog>
</div>
/* test.css */
.doc-page {
flex-direction: column;
background-color: #F1F3F5;
}
.btndiv {
width: 100%;
height: 200px;
flex-direction: column;
align-items: center;
justify-content: center;
}
.btn {
radius:60px;
font-size: 100px;
background-color: #1E90FF;
}
.schedulediv {
width: 100%;
height: 200px;
flex-direction: column;
justify-content: space-around;
padding-left: 55px;
}
.text1 {
color: #000000;
font-weight: bold;
font-size: 39px;
}
.text2 {
color: #a9a9a9;
font-size: 30px;
}
.dialogdiv {
flex-direction: column;
align-items: center;
}
.innertxt {
width: 320px;
height: 160px;
flex-direction: column;
align-items: center;
justify-content: space-around;
}
.text3 {
font-family: serif;
color: #1E90FF;
font-size: 38px;
}
.text4 {
color: #a9a9a9;
font-size: 33px;
}
.area {
width: 320px;
border-bottom: 1px solid #1E90FF;
}
.innerbtn {
width: 320px;
height: 120px;
justify-content: space-around;
}
.btntxt {
text-color: #1E90FF;
}
/* test.js */
var info = null;
import prompt from '@system.prompt';
import router from '@system.router';
export default {
data: {
curYear:'',
curMonth:'',
curDay:'',
date:'',
schedule:'',
schedulelist:[]
},
onInit() {
// Obtain the current date.
var date = new Date();
this.curYear = date.getFullYear();
this.curMonth = date.getMonth() + 1;
this.curDay = date.getDate();
this.date = this.curYear + '-' + this.curMonth + '-' + this.curDay;
this.schedulelist = []
},
addschedule(e) {
this.$element('datedialog').show()
},
canceldialog(e) {
prompt.showToast({
message: 'Event setting canceled.'
})
},
getschedule(e) {
info = e.value
},
cancelschedule(e) {
this.$element('datedialog').close()
prompt.showToast({
message: 'Event setting canceled.'
})
},
// Touch OK to save the data.
setschedule(e) {
if (e.text === '') {
this.schedule = info
} else {
this.schedule = info
var addItem = {schedule: this.schedule,}
this.schedulelist.push(addItem)
}
this.$element('datedialog').close()
}
}
编辑
3 -> form
Form是一个表单容器,支持容器内Input组件内容的提交和重置。
说明
从API Version 6开始支持。
3.1 -> 创建Form组件
在pages/index目录下的hml文件中创建一个Form组件。
<!-- test.hml -->
<div class="container">
<form style="width: 100%; height: 20%">
<input type="text" style="width:80%"></input>
</form>
</div>
/* test.css */
.container {
width:100%;
height:100%;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #F1F3F5;
}
编辑
3.2 -> 实现表单缩放
为Form组件添加click-effect属性,实现点击表单后的缩放效果。
<!-- test.hml -->
<div class="container">
<form id="formId" class="formClass" click-effect="spring-large">
<input type="text"></input>
</form>
</div>
3.3 -> 设置Form样式
通过为Form添加background-color和border属性,来设置表单的背景颜色和边框。
/* test.css */
.container {
width: 100%;
height: 100%;
flex-direction: column;
align-items: center;
justify-content: center;
background-color: #F1F3F5;
}
.formClass{
width: 80%;
height: 100px;
padding: 10px;
border: 1px solid #cccccc;
}
编辑
3.4 -> 添加响应事件
为Form组件添加submit和reset事件,来提交表单内容或重置表单选项。
<!-- test.hml -->
<div class="container">
<form onsubmit='onSubmit' onreset='onReset' class="form">
<div style="width: 100%;justify-content: center;">
<label>Option 1</label>
<input type='radio' name='radioGroup' value='radio1'></input>
<label>Option 2</label>
<input type='radio' name='radioGroup' value='radio2'></input>
</div>
<div style="width: 100%;justify-content: center; margin-top: 20px">
<input type="submit" value="Submit" style="width:120px; margin-right:20px;" >
</input>
<input type="reset" value="Reset" style="width:120px;"></input>
</div>
</form>
</div>
/* index.css */
.container{
width: 100%;
height: 100%;
flex-direction: column;
justify-items: center;
align-items: center;
background-color: #F1F3F5;
}
.form{
width: 100%;
height: 30%;
margin-top: 40%;
flex-direction: column;
justify-items: center;
align-items: center;
}
/* test.js */
import prompt from '@system.prompt';
export default{
onSubmit(result) {
prompt.showToast({
message: result.value.radioGroup
})
},
onReset() {
prompt.showToast({
message: 'Reset All'
})
}
}
编辑
3.5 -> 场景示例
在本场景中,可以选择相应选项并提交或重置数据。
创建Input组件,分别设置type属性为checkbox(多选框)和radio(单选框),再使用Form组件的onsubmit和onreset事件实现表单数据的提交与重置。
<!-- test.hml -->
<div class="container">
<form onsubmit="formSubmit" onreset="formReset">
<text style="font-size: 30px; margin-bottom: 20px; margin-top: 100px;">
<span > Form </span>
</text>
<div style="flex-direction: column;width: 90%;padding: 30px 0px;">
<text class="txt">Select 1 or more options</text>
<div style="width: 90%;height: 150px;align-items: center;justify-content: space-around;">
<label target="checkbox1">Option 1</label>
<input id="checkbox1" type="checkbox" name="checkbox1"></input>
<label target="checkbox2">Option 2</label>
<input id="checkbox2" type="checkbox" name="checkbox2"></input>
</div>
<divider style="margin: 20px 0px;color: pink;height: 5px;"></divider>
<text class="txt">Select 1 option</text>
<div style="width: 90%;height: 150px;align-items: center;justify-content: space-around;">
<label target="radio1">Option 1</label>
<input id="radio1" type="radio" name="myradio"></input>
<label target="radio2">Option 2</label>
<input id="radio2" type="radio" name="myradio"></input>
</div>
<divider style="margin: 20px 0px;color: pink;height: 5px;"></divider>
<text class="txt">Text box</text>
<input type="text" placeholder="Enter content." style="margin-top: 50px;"></input>
<div style="width: 90%;align-items: center;justify-content: space-between;margin: 40px;">
<input type="submit">Submit</input>
<input type="reset">Reset</input>
</div>
</div>
</form>
</div>
/* index.css */
.container {
width: 100%;
height: 100%;
flex-direction:column;
align-items:center;
background-color:#F1F3F5;
}
.txt {
font-size:33px;
font-weight:bold;
color:darkgray;
}
label{
font-size: 20px;
}
/* test.js */
import prompt from '@system.prompt';
export default {
formSubmit() {
prompt.showToast({
message: 'Submitted.'
})
},
formReset() {
prompt.showToast({
message: 'Reset.'
})
}
}
编辑
感谢各位大佬支持!!!
互三啦!!!
相关推荐
- APP及网站快速变黑白灰色的方法(2020网站变黑白)
-
很多人在打开支付宝、淘宝、美团、今日头条、小红书等软件APP的时候,都会发现首页变成了黑白色吧朋友圈里都是大家的纪念、歌颂、缅怀;像父母那代人更是感情真诚热烈,对于他们来说,江老的离开也像是一个时代的...
- 你了解所有ND滤镜吗?(nd滤镜测评)
-
风光好伙伴?延时好伴侣?今天给大家分享一下ND滤镜。如有不足,欢迎交流。TheNeutralDensityfilter简称ND减光镜,也叫中密度灰减光滤镜。它的作用是减少进入镜头的光量。根据通光...
- 滤镜Shader——LookUpTable(slumber滤镜)
-
LookUpTable(简称LUT,颜色查找表)是一种降低GPU运算量的技术,通过事先将颜色值存储在一张缓存表中当需要运算的时候直接从这张表中索引出对应的颜色值,本质上可看做计算机领域常见的存储空...
- 【FFmpeg笔记】 从零开始之滤镜(ffmpeg filter_complex)
-
FFmpeg除了具有强大的封装/解封装、编/解码功能外,还包含了一个非常强大的组建---滤镜avfilter。avfilter组建经常用于进行多媒体的处理与编辑,FFmpeg中包含多种滤镜1.FFm...
- 原来ND镜应该这么用(nd镜使用技巧)
-
为什么要使用滤镜?数码时代,相机的功能已经非常强大,后期软件也具有神乎其神的功能,那么滤镜是否还有存在的必要呢?一些色彩滤镜、色温矫正滤镜、特效滤镜(如星光镜、速度镜、柔焦镜等)确实没有太大存在的必要...
- CPL偏光镜、ND减光镜使用教程(减光镜偏振镜的区别)
-
要拍好照片,除了需具备相机基本光圈、快门、ISO…..等操作技能外,还得适时搭配滤镜的使用,如此才为影像带来更丰富的视觉变化,以下我们就以常用的「偏光镜」、「减光镜」应用为例。C-PL偏光镜偏光镜即...
- PS 2021版更新强大黑科技,修图师要下岗了?
-
使用PS把左图修为右图,你猜猜用了几个步骤,用了多长时间?答案是仅仅用了三步,前后用时不到5分钟——套了几个PSCC2021的neuralfilter(神经网络滤镜),只需点点点就完成了包括磨皮...
- 4块摄影新手必备滤镜(摄影滤镜使用技巧)
-
刚开始学习摄影的摄友也常会看到「滤镜」(Filter)一词,其实现在有很多不同种类的滤镜,对于新手来说,又应该怎样选择购买必须的滤镜呢?它们又会在什么时间会用上呢?这篇文章会可以给各位一些小提示!A...
- Photoshop中英文菜单对照表(ps软件全英文)
-
Photoshop是Adobe公司旗下最为出名的图像处理软件之一,集图像扫描、编辑修改、图像制作、广告创意,图像输入与输出于一体的图形图像处理软件,深受广大平面设计人员和电脑美术爱好者的喜爱。由于该软...
- 四块摄影新手必备滤镜(四块摄影新手必备滤镜图片)
-
刚开始学习摄影的摄友也常会看到「滤镜」(Filter)一词,其实现在有很多不同种类的滤镜,对于新手来说,又应该怎样选择购买必须的滤镜呢?它们又会在什么时间会用上呢?这篇文章会可以给各位一些小提示!A...
- 为什么说高效创作还得看散热?(为什么说人的美是社会美的核心)
-
笔记本使用时间长,经常发热发烫?为此,创作者们想尽办法给笔记本降温防热,如清除内部灰尘、在下方加上散热底座等,但这些散热方法并不能有效解决发热,还需要依靠笔记本自身强大的散热系统,才可以使发热得到缓解...
- 网站都变成灰色,几行代码就搞定了
-
大家看到网站的内容都变成了灰色,包括按钮、图片等等。这时候我们可能会好奇这是怎么做到的呢?有人会以为所有的内容都统一换了一个CSS样式,图片也全换成灰色的了,按钮等样式也统一换成了灰色样式。但你想...
- 什么是ND滤镜?有什么具体用途?(nd滤镜和ndpl)
-
ND滤镜,全称为中性密度滤镜(NeutralDensityFilter),是一种可以减少光线进入镜头的摄影配件,它可以让摄影师在强光环境下使用更大的光圈或者更长的快门,从而实现一些特殊的摄影效果。...
- 你好,色彩:滤镜混搭 FilterGrid(色彩滤镜打开对视力有害吗)
-
手机摄影越来越热门的今天,大家对照片处理App的要求也越来越高了,我的意思是:人类已经不能停止修图了。无论你是追求完美的处女座还是贪新爱玩的双子座,用App处理过后的照片的确更有美感或更有趣(...
- CSS超炫加载动画设计、实现与实例讲解
-
借助CSS提供的animation与transform及filter滤镜等属性,我们可以使用CSS设置出精美的动画效果,进一步可以使用CSS对HTML页面基本元素、图标等进行动画设计,如按钮效果,页面...
- 一周热门
- 最近发表
- 标签列表
-
- HTML 教程 (33)
- HTML 简介 (35)
- HTML 实例/测验 (32)
- HTML 测验 (32)
- JavaScript 和 HTML DOM 参考手册 (32)
- HTML 拓展阅读 (30)
- HTML常用标签 (29)
- HTML文本框样式 (31)
- HTML滚动条样式 (34)
- HTML5 浏览器支持 (33)
- HTML5 新元素 (33)
- HTML5 WebSocket (30)
- HTML5 代码规范 (32)
- HTML5 标签 (717)
- HTML5 标签 (已废弃) (75)
- HTML5电子书 (32)
- HTML5开发工具 (34)
- HTML5小游戏源码 (34)
- HTML5模板下载 (30)
- HTTP 状态消息 (33)
- HTTP 方法:GET 对比 POST (33)
- 键盘快捷键 (35)
- 标签 (226)
- HTML button formtarget 属性 (30)
- CSS 水平对齐 (Horizontal Align) (30)