当前位置: 首页 > 科技观察

HarmonyOSArkUI-eTS常用控件

时间:2023-03-12 03:59:54 科技观察

更多信息请访问:与华为官方共建的Harmonyos技术社区https://harmonyos.51cto.com1.DisplayHidden控件支持版本:eTSAPIVersion7+属性名称:visibility功能:控制该属性所在组件的显示或隐藏默认值:Visible(显示)其他值:Hidden(隐藏,占用布局空间),None(隐藏,但不占用布局空间)使用示例:分别使用该属性的三个值定义四个按钮1、2、3、4。第一个按钮设置为显示,第二个按钮设置为隐藏和占用空间,第三个按钮设置为隐藏不占用空间。最后一个按钮4号用于判断上一个按钮是否占用布局空间。@Entry@ComponentstructSample{build(){Flex({direction:FlexDirection.Column,alignItems:ItemAlign.Center,justifyContent:FlexAlign.Center}){Text('Explicit').fontSize(30).fontWeight(FontWeight.Bold)Button('1').visibility(Visibility.Visible)Text('隐式占位符').fontSize(30).fontWeight(FontWeight.Bold)Button('2').visibility(Visibility.Hidden)Text('隐式不占空间').fontSize(30).fontWeight(FontWeight.Bold)Button('3').visibility(Visibility.None)Button('4').visibility(Visibility.Visible)}.width('100%').height('100%')}}如下图:2.禁用控件支持版本:eTSAPIVersion7+属性名:enabled功能:控制属性所在组件是否禁用Default:true(组件可用)其他值:false(组件不可??用)使用示例:以按钮为例,分别设置按钮禁用和按钮可用@Entry@ComponentstructSample{build(){Flex({direction:FlexDirection.Column,alignItems:ItemAlign.Center,justifyContent:FlexAlign.Center}){Button('Disablebutton').enabled(false).opacity(0.4).margin({底部:10})但是ton('Availablebutton').enabled(true)}.width('100%').height('100%')}}如下图:3.Zsequencecontrolsupportedversion:eTSAPIVersion7+attributename:zIndex功能:显示同一容器内兄弟组件的层级关系(叠加关系)默认值:0(底部)其他值:number类型n(放置在第n层)使用示例:主要使用堆叠容器Stack(见扩展内容在文末),在容器中添加相关组件,并使用Z-order控件对内容进行层次划分@Entry@ComponentstructSample{build(){Flex({direction:FlexDirection.Column,alignItems:ItemAlign.Center,justifyContent:FlexAlign.Center}){Stack(){Text('Zsequence--2').fontSize(15).size({width:'40%',height:'10%'}).backgroundColor(Color.Yellow).zIndex(2)//默认值0Text('default0').fontSize(15).size({width:'90%',height:'50%'}).backgroundColor(Color.Green).align(Alignment.TopStart)Text('Zsequence--1').fontSize(15).size({width:'70%',height:'30%'}).backgroundColor(Color.Blue).align(Alignment.TopStart).zIndex(1)}}.width('100%').height('100%')}}如下图所示:4.弹窗控件支持版本:eTSAPIVersion7+属性名称:bindPopup功能:将Popup绑定到组件上,点击后会出现弹窗的主要参数:show(默认是否显示弹窗提示,默认为false),popup(配置弹窗提示信息—PopupOption,CustomPopupOption两个接口)常用属性PopupOption接口的:message(弹窗信息内容),placementOnTop(是否显示在组件上方,默认值为false)CustomPopupOption接口的常用属性:placement(气泡先显示位置,自定义位置放不下时,会自动调整位置),popupColor(提示气泡的颜色)使用示例:@Entry@ComponentstructSample{@StatenoHandlePopup:boolean=falsebuild(){flex({direction:FlexDirection.Column,alignItems:ItemAlign.Center}){Button('button').width('50%').margin({top:100}).onClick(()=>{this.noHandlePopup=!this.noHandlePopup}).bindPopup(this.noHandlePopup,{message:'Pop-up1',//弹窗内容placement:Placement.Bottom,//弹窗在按钮底部onStateChange:(e)=>{console.info(e.isVisible.toString())if(!e.isVisible){this.noHandlePopup=false}}})}.width('100%').height('100%')}}如图下图:5.点击控制支持版本:eTSAPIVersion8+属性名:touchable功能:设置当前组件是否可触摸默认值:true使用示例:@Entry@ComponentstructSample{@Statetext_touch:string=''build(){Flex({方向:FlexDirection.Column,alignItems:ItemAlign.Center,justifyContent:FlexAlign.Center}){Circle().fill(Color.Orange).width(100).height(100).touchable(true).onClick(()=>{console.info(this.text_touch='Youhavealreadyclickedonthisgraphic')})。overlay(this.text_touch,{align:Alignment.Bottom,offset:{x:0,y:50}})}.width('100%').height('100%')}}如下图:6.StackContainer(基于Z-order控件展开组件)支持版本:eTSAPIVersion7+接口:Stack(value:{alignContent?:Alignment})参数alignContent:默认值Center(子组件在容器中的对齐方式)用法例子:。..........Stack({alignContent:Alignment.Bottom}){//设置为底部对齐Text('subcomponentone').fontSize(15).width('90%').height('100%').backgroundColor(Color.Blue).align(Alignment.Top)Text('subcomponenttwo').fontSize(15).width('70%').height('60%').backgroundColor(Color.Grey).align(Alignment.Top)}.width('100%').height(150).margin({top:5})}...............如下图所示:更多信息请访问:鸿蒙科技社区,与华为官方共建https://Harmonyos.51cto.com