鸿蒙(HarmonyOS)下拉选择控件

一、操作环境

操作系统: Windows 11 专业版、IDE:DevEco Studio 3.1.1 Release、SDK:HarmonyOS 3.1.0(API 9)

二、效果图

三、代码

复制代码
SelectPVComponent.ets
TypeScript 复制代码
@Component
export default struct SelectPVComponent {
  @Link selection: SelectOption[]
  private callback: (index: number, value?: string) => void
  private text: string

  build() {
    Row() {
      Image($r('app.media.required')).margin({ bottom: 5 }).width('5%')

      Text(this.text)
      //设置SelectOption对象参数
      Select(this.selection)
        .selected(0)
        .value('请选择')
        .font({ size: 16, weight: 500 })
        .selectedOptionFont({ size: 16, weight: FontWeight.Regular })
        .optionFont({ size: 16, weight: 400 })
        .onSelect((index: number, value: string) => {
          this.callback?.(index, value)
        })
    }.width('100%')
  }
}

在page中的调用方式:

TypeScript 复制代码
//问题程度
//若需要选项前带图标。可自定添加icon:{ value: '一般',icon:'xxx' }
@State issueExtent: SelectOption[] = [{ value: '一般' }, { value: '严重' }, { value: '紧要' }]

build() {
    Column() {
    SelectPVComponent({ text: '问题程度:',
            selection: $issueExtent,
            callback: (index: number, value: string) => {
              console.info('问题程度:' + index + ': ' + value)
            } })
    }
}
相关推荐
youtootech5 小时前
HarmonyOS 实战教程(八):个人中心与华为云服务集成 —— 以「柚兔自测量表」为例
华为·华为云·harmonyos
红烧大青虫6 小时前
HarmonyOS应用《玄象》开发实战:掷钱动画:animateTo + 缓动曲线的物理感模拟
harmonyos·鸿蒙
二流小码农7 小时前
鸿蒙开发:实现文本渐变效果
android·ios·harmonyos
程序员黑豆8 小时前
鸿蒙应用开发:Refresh + List 下拉刷新组件使用教程
前端·华为·harmonyos
fengxinzi_zack9 小时前
HarmonyOS应用《玄象》开发实战:MansionListPage 列表页:List / ListItem / LazyForEach 性能优化
harmonyos·鸿蒙
youtootech9 小时前
HarmonyOS 实战教程(九):响应式布局与断点系统 —— 以「柚兔自测量表」为例
华为·harmonyos
山璞9 小时前
将一个 Flutter 项目转为用 ArkUI-X 框架实现(4)
flutter·harmonyos
2501_9197490310 小时前
华为鸿蒙隐私文件加密APP—小羊加密室
华为·harmonyos
程序员黑豆10 小时前
鸿蒙开发实战:使用 List 组件构建新闻列表
前端·华为·harmonyos
爱写代码的阿森11 小时前
鸿蒙三方库 | harmony-utils之RegexUtil正则匹配验证详解
服务器·华为·harmonyos·鸿蒙·huawei