HarmonyOS鸿蒙Next中是否有同学用过material-dialogs三方库?能否提供一个demo

HarmonyOS鸿蒙Next中是否有同学用过material-dialogs三方库?能否提供一个demo 有同学用过material-dialogs三方库吗?能否提供一个demo,感谢!!!

3 回复
看看行不行

```typescript
import { ClickCallback, DialogAttributeModel, MaterialDialog, SingleChoiceListener } from '[@ohos](/user/ohos)/material-dialogs';

[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index {
  [@State](/user/State) message: string = 'Hello World';
  model: MaterialDialog.Model = new MaterialDialog.Model();
  dialogAttribute = new DialogAttributeModel()
  [@State](/user/State) items: string[] = ['男', '女']
  [@State](/user/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](/user/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: ClickCallback1 = 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
  }
}

更多关于HarmonyOS鸿蒙Next中是否有同学用过material-dialogs三方库?能否提供一个demo的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,material-dialogs 是一个基于 Android 的第三方库,主要用于创建 Material Design 风格的对话框。由于鸿蒙OS与Android系统的架构和API存在差异,直接使用 material-dialogs 并不适用于鸿蒙OS。

鸿蒙OS提供了自己的UI框架和组件库,开发者可以使用鸿蒙的 Dialog 组件来创建对话框。鸿蒙的 Dialog 组件支持自定义样式和布局,能够满足大多数对话框需求。以下是一个简单的鸿蒙对话框示例:

import { AlertDialog, Button } from '@ohos.arkui.advanced';

@Entry
@Component
struct Index {
  @State isDialogShow: boolean = false;

  build() {
    Column() {
      Button('Show Dialog')
        .onClick(() => {
          this.isDialogShow = true;
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
    .backgroundColor(0xF1F3F5)

    if (this.isDialogShow) {
      AlertDialog({
        title: '提示',
        message: '这是一个鸿蒙对话框示例',
        confirm: {
          value: '确定',
          action: () => {
            this.isDialogShow = false;
          }
        },
        cancel: {
          value: '取消',
          action: () => {
            this.isDialogShow = false;
          }
        }
      })
    }
  }
}

该示例使用鸿蒙的 AlertDialog 组件创建了一个简单的对话框,包含标题、消息以及确认和取消按钮。开发者可以根据需要进一步自定义对话框的样式和功能。

在HarmonyOS鸿蒙Next中,material-dialogs是一个常用的Android UI库,用于创建Material Design风格的对话框。虽然鸿蒙OS与Android有差异,但通过适配,可以在鸿蒙中使用。以下是一个简单的material-dialogs使用示例:

import com.afollestad.materialdialogs.MaterialDialog;

new MaterialDialog.Builder(context)
    .title("提示")
    .content("这是一个Material Dialog示例")
    .positiveText("确定")
    .negativeText("取消")
    .show();

请确保在build.gradle中添加依赖:

implementation 'com.afollestad.material-dialogs:core:0.9.6.0'

注意:鸿蒙Next可能需要额外适配,建议参考官方文档进行兼容性测试。

回到顶部