全局UI方法-弹窗三-文本滑动选择器弹窗(TextPickDialog)

文章介绍了如何在应用中创建TextPickerDialog文本选择器,包括其接口、参数配置(如选择范围、默认选中项、回调函数等),并提供了示例代码展示了如何在不同场景下使用这个组件。
摘要由CSDN通过智能技术生成

1、描述

        根据指定的选择范围创建文本选择器,展示在弹窗上。

2、接口

        TextPickDialog(options?: TextPickDialogOptions)

3、TextPickDialogOptions

参数名称

参数类型

必填

参数描述

rang

string[] | Resource

设置文本选择器的选择范围。

selected

number

设置选中项的索引值。默认值:0。

valuestring设置选中项的文本内容。当设置了selected参数时,该参数不生效。如果设置的value值不在range范围内,则默认去range第一个元素。
DefaultPickItemHeightnumber | string设置选择器中选项的高度。
onAccept(value : TextPickResult) => void点击弹窗中“确定”按钮时触发该回调。
onCancel() => void点击弹窗中“取消”按钮时触发该回调。
onChange(value : TextPickResult) => void滑动弹窗中的选择器使当前选中项改变时触发该回调。

4、TextPickerResult对象说明

类型

描述

value

string

选中项的文本内容。

index

number

选中项在选择范围数组中的索引值。

5、参数解析

1.当selected和value参数都设置时,且selecetd设置值在范围内时,value参数无效。

2.当不设置selected参数时,并且设置的value值不在range范围内时,显示的是默认的range的第一个元素。

3.根据onAccept和onChange监听或者确定选中项的数据信息。

6、示例

@Entry
@Component
struct TextPickerDialogPage {
  @State message: string = '根据指定的选择范围创建文本选择器,展示在弹窗上。'
  private defaultSelectIndex: number = 2; // 默认选中项索引值
  private developList: string[] = ['Android', 'IOS', 'Java', 'ArkTS', 'ArkUI-X', 'Python', 'TypeScript', 'Vue', 'HarmonyOS']; // 弹窗选项列表

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(20)
          .fontWeight(FontWeight.Bold)
          .width("96%")
          .margin({ top: 12 })

        Button("TextPickerDialog.show Default")
          .width("96%")
          .fontSize(20)
          .margin({ top: 12 })
          .onClick(() => {
            TextPickerDialog.show({
              range: this.developList,
              onAccept: (result: TextPickerResult) => {
                console.info("TextPickerDialog Default onAccept result = " + JSON.stringify(result));
              },
              onCancel: () => {
                console.info("TextPickerDialog Default onCancel");
              },
              onChange: (result: TextPickerResult) => {
                console.info("TextPickerDialog Default onChange result = " + JSON.stringify(result));
              }
            })
          })

        Button("TextPickerDialog.show Selected")
          .width("96%")
          .fontSize(20)
          .margin({ top: 12 })
          .onClick(() => {
            TextPickerDialog.show({
              range: this.developList,
              selected: this.defaultSelectIndex,
              value: "Vue", // 当设置selected参数时,value参数会失效
              defaultPickerItemHeight: 60,
              onAccept: (result: TextPickerResult) => {
                console.info("TextPickerDialog Selected onAccept result = " + JSON.stringify(result));
              },
              onCancel: () => {
                console.info("TextPickerDialog Selected onCancel");
              },
              onChange: (result: TextPickerResult) => {
                console.info("TextPickerDialog Selected onChange result = " + JSON.stringify(result));
              }

            })
          })

      }
      .width('100%')
      .height("100%")
    }
    .height('100%')
  }
}

7、效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyxhzdm

你的鼓励是我创作的最大动力!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值