扩展鸿蒙textinput组件

简介: 扩展鸿蒙textinput组件

扩展鸿蒙textinput组件,支持快速扩展展性,标题文本等,文本内容双向绑定、文本组件快速复用。

组件代码

/**
 * 单选文本
 */
@Component
export default  struct DiygwInput{
  //绑定的值
  @Link value:string;
  //未选中图标
  @State labelImg: Resource = $r('app.media.user');
  //是否文本图片
  @State isLabelImg: boolean = false;
  @State labelImgWidth: number = 20;
  @State labelImgHeight: number = 20;
  //标题文本
  @State label:string = '文本';
  //水平状态时,文本占大小
  @State labelWidth:number = 80;
  //是否标题文本换行
  @State isWrapLabel:boolean = false;
  //是否标题文本
  @State isLabel:boolean = true;
  //标题颜色
  @State labelColor:string = "#333333";
  //自动标题长度
  @State isLabelAuto:boolean = false;
  //文本字体大小
  @State textSize:number = 14;
  //选中图版本大小
  @State imgSize:number = 28;
  //每个占比
  @State itemWidth:string = '33%';
  //组件内边距
  @State leftRightPadding:number = 16;
  @State topBottomPadding:number = 6;
 
  @State placeHolder:string = '请输入'
 
  @State placeHolderColor:string = '#fff'
 
  @State inputRadius:number = 2;
  @State textHeight:number = 30
  @State isBorder:boolean = true;
  @State inputType:InputType = InputType.Normal
 
 
  build() {
    Flex({
      alignItems:this.isWrapLabel?ItemAlign.Start:ItemAlign.Center,
      direction:this.isWrapLabel?FlexDirection.Column:FlexDirection.Row,
      justifyContent:FlexAlign.Start
    }){
      if(this.isLabel){
        Row(){
          if(this.isLabelImg){
            Image(this.labelImg)
              .width(this.labelImgWidth)
              .height(this.labelImgHeight)
              .margin({ left:3 }).flexShrink(0)
          }
          if(this.isLabelAuto){
            Text(this.label).flexShrink(0).fontColor(this.labelColor).fontSize(this.textSize).margin({
              bottom:this.isWrapLabel?10:0,
              right:10,
            }).textAlign(TextAlign.Start);
          }else{
            Text(this.label).fontColor(this.labelColor).width(this.isWrapLabel?'100%':this.labelWidth).fontSize(this.textSize).margin({
              bottom:this.isWrapLabel?10:0
            }).textAlign(TextAlign.Start);
          }
        }.margin({
          top:this.isWrapLabel?10:0
        })
      }
 
      TextInput({ placeholder: this.placeHolder,text:this.value })
        .fontSize(this.textSize)
        .type(this.inputType)
        .borderRadius(this.inputRadius)
        .padding({ left:this.isWrapLabel?3:10 })
        .placeholderColor(this.placeHolderColor)
        .backgroundColor(Color.White)
        .fontWeight(FontWeight.Normal)
        .fontStyle(FontStyle.Normal)
        .fontColor(Color.Black).height(this.textSize+15).width('100%') .onChange((value: string) => {
          this.value = value;
        })
    }.borderWidth({
      bottom: this.isBorder?1:0
    }).borderColor({
      bottom: "#eee"
    }).borderStyle(BorderStyle.Solid).borderStyle(BorderStyle.Solid).height(this.textSize+(this.isWrapLabel?20:0)+30+this.topBottomPadding*2).padding({left:this.leftRightPadding,right:this.leftRightPadding,top:this.topBottomPadding,bottom:this.topBottomPadding})
    // .onAppear(() => {
    //   this.initCheck()
    // })
  }
}

快速实现输入组件调用代码

import {
    navigateTo
} from '../common/Page'
import {
    IDynamicObject
} from '../component/IType';
import DiygwInput from '../component/Input'
@Entry
@Component
export struct Form {
    @State input: string = '';
    @State input1: string = '';
    @State input2: string = '';
 
    async onPageShow() {}
 
    async aboutToAppear() {
        await this.onPageShow()
    }
 
    build() {
        Row() {
            Navigation() {
                    Column() {
                        Scroll() {
                            Column() {
                                DiygwInput({
                                        label: '标题',
                                        placeHolder: '请输入标题',
                                        value: $input
                                    })
                                    .width('100%')
                                DiygwInput({
                                        label: '标题',
                                        placeHolder: '请输入密码',
                                        inputType: InputType.Password,
                                        value: $input1
                                    })
                                    .width('100%')
                                DiygwInput({
                                        label: '标题',
                                        placeHolder: '请输入电话',
                                        inputType: InputType.PhoneNumber,
                                        value: $input2
                                    })
                                    .width('100%')
                            }.alignItems(HorizontalAlign.Start)
                        }
                    }.height('100%').alignItems(HorizontalAlign.Start).backgroundColor('#fff')
 
                }
                .width('100%')
                .height('100%')
                .backgroundColor('#07c160')
                .title(this.NavigationTitle())
                .titleMode(NavigationTitleMode.Mini)
                .align(Alignment.Center)
                .hideBackButton(true)
        }.width('100%').height('100%')
    }
 
    @Builder
    NavigationTitle() {
        Column() {
            Text('表单')
                .width('100%')
                .textAlign(TextAlign.Center)
                .height('28vp')
                .fontSize('20fp')
                .fontWeight(500)
                .fontColor('#fff')
        }
    }
 
 
}


目录
相关文章
|
6月前
|
开发者 容器
鸿蒙应用开发从入门到实战(十四):ArkUI组件Column&Row&线性布局
ArkUI提供了丰富的系统组件,用于制作鸿蒙原生应用APP的UI,本文主要讲解Column和Row组件的使用以及线性布局的方法。
527 12
|
6月前
|
API 数据处理
鸿蒙应用开发从入门到实战(十三):ArkUI组件Slider&Progress
ArkUI提供了丰富的系统组件,用于制作鸿蒙原生应用APP的UI,本文主要讲解滑块Slider和进度条Progress组件的使用。
263 1
|
9月前
|
JavaScript 开发工具 开发者
【HarmonyOS 5】鸿蒙Web组件和内嵌网页双向通信DEMO示例
【HarmonyOS 5】鸿蒙Web组件和内嵌网页双向通信DEMO示例
373 3
|
9月前
|
开发者
鸿蒙仓颉开发语言实战教程:自定义组件
本文介绍了如何在仓颉开发语言中创建自定义组件,以封装和管理项目中的 tabbar 组件为例。通过创建独立的组件文件 yltabbar.cj,并使用 @Component 和 @Link 等修饰符实现组件化开发与参数传递,提升代码复用性和项目可维护性。适合希望深入掌握仓颉语言组件开发的 HarmonyOS 开发者学习参考。 #HarmonyOS #仓颉 #购物
|
缓存 数据安全/隐私保护 JavaScript
【HarmonyOS 5】鸿蒙页面和组件生命周期函数
【HarmonyOS 5】鸿蒙页面和组件生命周期函数
375 0
|
9月前
|
存储 IDE 定位技术
【HarmonyOS 5】鸿蒙组件&模板服务详解 - 助力高效开发的利器
在移动应用开发领域,效率与质量始终是开发者追求的核心目标。鸿蒙系统作为新兴的操作系统,为开发者提供了丰富且强大的开发资源,其中鸿蒙组件&模板服务更是成为开发者快速构建高质量应用的得力助手。
303 0
鸿蒙仓颉语言开发教程:页面和组件的生命周期
仓颉语言中的生命周期指页面或组件从加载到消失的过程。与ArkTs不同,仓颉需在生命周期方法前添加`protected open`修饰符,如`aboutToAppear()`、`onPageShow()`等。部分函数如`onBackPress()`返回布尔值,决定是否拦截系统返回操作。仅`@Entry`组件支持全部生命周期,普通组件仅支持`aboutToAppear`和`aboutToDisappear`。掌握正确写法可避免踩坑。
|
9月前
|
缓存
鸿蒙5开发宝藏案例分享---Swiper组件性能优化实战
本文分享了鸿蒙系统中Swiper组件的性能优化技巧,包括:1) 使用`LazyForEach`替代`ForEach`实现懒加载,显著降低内存占用;2) 通过`cachedCount`精准控制缓存数量,平衡流畅度与内存消耗;3) 利用`onAnimationStart`在抛滑时提前加载资源,提升构建效率;4) 添加`@Reusable`装饰器复用组件实例,减少创建开销。实际应用后,图库页帧率从45fps提升至58fps,效果显著。适合处理复杂列表或轮播场景,欢迎交流经验!
|
6月前
|
数据安全/隐私保护 开发者
鸿蒙应用开发从入门到实战(十一):ArkUI组件Text&TextInput
ArkUI提供了丰富的系统组件,用于制作鸿蒙原生应用APP的UI,本文主要讲解文本组件Text和TextInput的使用。
410 3
|
6月前
|
API 数据处理
鸿蒙应用开发从入门到实战(十三):ArkUI组件Slider&Progress
ArkUI提供滑块Slider与进度条Progress组件,用于鸿蒙原生APP开发。Slider支持拖动调节音量、亮度等,可设步长、方向及提示气泡;Progress支持线性、环形等多种样式,可自定义颜色、宽度与刻度,实时显示任务进度。
384 2