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 
  } 
}
HarmonyOS
2024-11-19 10:43:47
浏览
收藏 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 
  } 
}
分享
微博
QQ
微信
回复
2024-11-19 15:46:18
相关问题
图片剪切的第三方框架
228浏览 • 1回复 待解决
如何获取可用的三方
1769浏览 • 1回复 待解决
三方应用如何获取蓝牙mac地址
2253浏览 • 1回复 待解决
三方应用如何获取http代理信息
1762浏览 • 1回复 待解决
鸿蒙有哪些支持的第三方UI框架吗?
3588浏览 • 1回复 待解决
三方应用使用蓝牙功能
991浏览 • 1回复 待解决
三方相机预览demo开发
891浏览 • 1回复 待解决
实现弹幕功能的三方
1090浏览 • 1回复 待解决
native交叉编译第三方
210浏览 • 1回复 待解决
开发第三方SDK如何编译?
13544浏览 • 2回复 待解决