material-dialogs三方框架获取值

使用material-dialogs弹窗,选择了一个item,获取到选择的值在组件外的类中方法onSelected中,怎么把选择的值传给组件中的sex?代码如下:

import { TopNavigationView } from '@ohos/uicomponents/Index'; 
import { router } from '@kit.ArkUI'; 
import UserManager from '@ohos/datastory/src/main/ets/datasource/userdata/UserManager'; 
import { getTeacherSelfInfo } from '../model/AccountMagNetModel'; 
import { Logger } from '@ohos/utils/Index'; 
import { CustomDialog1, showToast } from '@ohos/base/Index'; 
import { GetTeacherDataBean } from '../entity/GetTeacherSelfInfoModel'; 
import { AccountMagItem } from '../components/AccountMagItem'; 
import { ClickCallback, DialogAttributeModel, MaterialDialog, 
  SingleChoiceListener } from '@ohos/material-dialogs'; 
 
/*** 
 * 账号管理 
 */ 
@Entry({ routeName: 'AccountMagPage' }) 
@Component 
export struct AccountMagPage { 
  @State avatarImage: ResourceStr = $r("app.media.head_image") 
  @State name: string | undefined = '' 
  @State sex: ResourceStr = $r("app.string.man") 
  @State identity: ResourceStr = $r("app.string.parent") 
  @State loginAccount: ResourceStr = '' 
  @State organizInfo: ResourceStr = '' 
  @State bindWeChat: ResourceStr = $r('app.string.unbind') 
  isTeacher = false 
  @State teacherData: GetTeacherDataBean | undefined = undefined 
  model: MaterialDialog.Model = new MaterialDialog.Model(); 
  dialogAttribute = new DialogAttributeModel() 
  @State items: string[] = ['男', '女'] 
  private listenser:SingleChoiceListener = new SingleChoiceListener1() 
 
  existDialog() { 
    this.dialogController.close() 
  } 
 
  dialogController: CustomDialogController = new CustomDialogController({ 
    builder: MaterialDialog({ 
      model: this.model, dialogAttribute: this.dialogAttribute 
    }), 
    cancel: this.existDialog, 
    autoCancel: true, 
    alignment: DialogAlignment.Center, 
    customStyle: true 
  }) 
 
 
  aboutToAppear(): void { 
    this.getAvatarImage(); 
    this.getTeacherInfo(); 
  } 
 
  /** 
   *家长和老师端修改个人信息详情 
   */ 
  private getTeacherInfo() { 
    getTeacherSelfInfo().then( 
      (data) => { 
        this.teacherData = data; 
        this.sex = data.sex 
        this.identity = data.shenfen 
        this.loginAccount = data.zhanghao 
 
      } 
    ).catch((error: string) => { 
      Logger.error('changePwd:' + error); 
      showToast(error); 
    }); 
  } 
 
  /** 
   *获取基础信息 
   */ 
  private getAvatarImage() { 
    this.name = UserManager.getUserInfo()?.name; 
    this.isTeacher = UserManager.isTeacher(); 
    if (this.isTeacher) { 
      const teacherInfo = UserManager.getTeacherInfo(); 
      this.organizInfo = `${teacherInfo.schoolname}/${teacherInfo.gradename}/${teacherInfo.classname}`; 
      let key = "headerImage/xxx/xxx" + UserManager.getUserInfo()?.UserId + ".jpeg"; 
      const aa: number = Math.floor(Math.random() * 99999999); 
      this.avatarImage = "http://www.huawei.com/" + key + "?" + aa; 
    } else { 
      const studentInfo = UserManager.getStudentInfo(); 
      this.organizInfo = `${studentInfo.schoolName}/${studentInfo.gradeName}/${studentInfo.className}`; 
      this.avatarImage = studentInfo.headUrl; 
    } 
  } 
 
  build() { 
    Scroll() { 
      Column() { 
        TopNavigationView({ 
          title: $r('app.string.accountManagement'), 
          onBackClick: () => { 
            router.back() 
          } 
        }) 
        Column() { 
          this.textImgItem($r('app.string.avatar'), this.avatarImage) 
          Divider() 
          AccountMagItem({ 
            leftText: $r('app.string.name'), 
            rightText: this.name 
          }) 
 
          if (this.sex) { 
            Divider() 
            AccountMagItem({ 
              leftText: $r('app.string.sex'), 
              rightText: this.sex, 
              rightImg: $r('app.media.qianjin') 
            }).onClick(() => { 
              //弹出dialog 
              this.model.reset() 
              this.model.listItemsSingleChoice(this.items, 2, true, null, -1, -1, this.listenser) 
              this.model.positiveButton($r("app.string.sure"), Callback('', 0)) 
              this.model.negativeButton($r("app.string.cancel"), Callback('', 1)) 
              this.model.setStacked(false) 
              this.dialogController.open() 
 
 
            }) 
          } 
          Divider() 
          AccountMagItem({ 
            leftText: $r('app.string.identity'), 
            rightText: this.identity, 
          }) 
 
          AccountMagItem({ 
            leftText: $r('app.string.loginAccount'), 
            rightText: this.loginAccount, 
          }).margin({ 
            top: 5 
          }) 
          this.textOtherGray(5, $r('app.string.organizational'), $r('app.string.more')) 
          AccountMagItem({ 
            leftText: this.organizInfo, 
          }) 
          AccountMagItem({ 
            leftText: $r('app.string.infoUpdate'), 
            rightImg: $r('app.media.qianjin') 
          }).margin({ 
            top: 15 
          }).onClick(() => { 
 
          }) 
          Divider() 
          AccountMagItem({ 
            leftText: $r('app.string.bindWeChat'), 
            rightText: this.bindWeChat, 
            rightImg: $r('app.media.qianjin') 
          }).onClick(() => { 
 
          }) 
 
          Button($r('app.string.sign_out'), { type: ButtonType.Capsule, stateEffect: true }) 
            .width('80%') 
            .height(48) 
            .backgroundColor($r('app.color.color_primary')) 
            .margin({ 
              top: 50 
            }) 
            .onClick(() => { 
              //TODO:退出账号 
 
            }) 
 
 
        } 
        .height('100%') 
        .width('100%') 
        .backgroundColor('#cae7e2e2') 
      } 
      .height('100%') 
      .width('100%') 
    } 
  } 
 
  @Builder 
  textOtherGray(id: number, leftText: ResourceStr, rightText?: ResourceStr, rightImg?: ResourceStr) { 
    Row() { 
      Text(leftText).fontColor(Color.Grey) 
      Blank(); 
      if (rightText) { 
        Text(rightText) 
          .fontColor(Color.Green) 
          .margin({ 
            right: 10 
          }) 
      } 
      if (rightImg) { 
        Image(rightImg) 
          .objectFit(ImageFit.Contain) 
          .width($r('app.float.list_image_margin_right')) 
          .height($r('app.float.list_image_margin_right')) 
      } 
 
    } 
    .height(48) 
    .width('100%') 
    .alignItems(VerticalAlign.Center) 
    .padding({ 
      left: 20, 
      right: 20 
    }) 
    .onClick(() => { 
 
    }) 
  } 
 
  @Builder 
  textImgItem(leftText: ResourceStr, rightImg: ResourceStr) { 
    Row() { 
      Text(leftText); 
      Blank(); 
      Image(rightImg) 
        .width(60) 
        .height(60) 
        .borderRadius(100) 
        .clip(new Circle({ width: 60, height: 60 })); 
 
    } 
    .height(65) 
    .width('100%') 
    .alignItems(VerticalAlign.Center) 
    .backgroundColor('#ffffff') 
    .padding({ 
      left: 20, 
      right: 20 
    }) 
    .onClick(() => { 
 
    }) 
  } 
 
} 
 
class SingleChoiceListener1 implements SingleChoiceListener { 
  onSelected(value: string, index: number) { 
 
  } 
} 
 
function Callback(value1: string, type: number): ClickCallback { 
  let back: ClickCallback = new ClickCallback1(value1, type) 
  return back 
} 
 
class ClickCallback1 implements ClickCallback { 
  value1: string = '' 
  type: number = 0 
 
  onClick(value?: string) { 
    if (this.type === 0) { 
      console.info(this.value1) 
    } else if (this.type === 1) { 
      console.info('ClickCallback when the confirm button is clicked') 
    } else if (this.type === 2) { 
    } else { 
      console.info('ClickCallback when the confirm button is clicked') 
    } 
  } 
 
  constructor(value: string, type: number) { 
    this.value1 = value 
    this.type = type 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
HarmonyOS
2024-11-19 10:43:47
3164浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

请参考以下代码:

import { ClickCallback, DialogAttributeModel, MaterialDialog, SingleChoiceListener } from '@ohos/material-dialogs'; 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
  model: MaterialDialog.Model = new MaterialDialog.Model(); 
  dialogAttribute = new DialogAttributeModel() 
  @State items: string[] = ['男', '女'] 
  @State listenser: SingleChoiceListener1 = new SingleChoiceListener1() 
 
  existDialog() { 
    this.dialogController.close() 
  } 
 
  dialogController: CustomDialogController = new CustomDialogController({ 
    builder: MaterialDialog({ 
      model: this.model, dialogAttribute: this.dialogAttribute 
    }), 
    cancel: this.existDialog, 
    autoCancel: true, 
    alignment: DialogAlignment.Center, 
    customStyle: true 
  }) 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.listenser.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            //1. 弹出dialog 
            this.model.reset() 
            this.model.listItemsSingleChoice(this.items, 2, true, null, -1, -1, this.listenser) 
            this.model.positiveButton('确定', Callback('', 0)) 
            this.model.negativeButton('取消', Callback('', 1)) 
            this.model.setStacked(false) 
            this.dialogController.open() 
          }) 
 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
 
} 
 
 
class SingleChoiceListener1 implements SingleChoiceListener { 
  @Track message: string | Resource | undefined; 
  onSelected(value: string, index: number) { 
    //2. 获取到选择的性别(value = 男/女) 
    //3. 需要把value值传给message属性,然后刷新界面,显示选择的性别 
    this.message = value; 
  } 
} 
 
function Callback(value1: string, type: number): ClickCallback { 
  let back: ClickCallback = new ClickCallback1(value1, type) 
  return back 
} 
 
class ClickCallback1 implements ClickCallback { 
  value1: string = '' 
  type: number = 0 
 
  onClick(value?: string) { 
    if (this.type === 0) { 
      console.info(this.value1) 
    } else if (this.type === 1) { 
      console.info('ClickCallback when the confirm button is clicked') 
    } 
  } 
 
  constructor(value: string, type: number) { 
    this.value1 = value 
    this.type = type 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
分享
微博
QQ
微信
回复
2024-11-19 15:46:18


相关问题
图片剪切的第三方框架
1043浏览 • 1回复 待解决
如何获取可用的三方
2534浏览 • 1回复 待解决
三方应用如何获取蓝牙mac地址
3302浏览 • 1回复 待解决
三方应用如何获取http代理信息
2691浏览 • 1回复 待解决
HarmonyOS 请提供rn框架和第三方库源
891浏览 • 1回复 待解决
鸿蒙有哪些支持的第三方UI框架吗?
4782浏览 • 1回复 待解决
HarmonyOS 打开三方应用
930浏览 • 1回复 待解决
HarmonyOS 如何删除三方
1065浏览 • 1回复 待解决
HarmonyOS 推荐的三方风控
797浏览 • 1回复 待解决
三方应用使用蓝牙功能
1939浏览 • 1回复 待解决
HarmonyOS flutter三方plugin适配进展
811浏览 • 1回复 待解决