当前位置:   article > 正文

【HarmonyOS】笔记六-对话框_promptaction.showtoast

promptaction.showtoast

概念

对话框在任何一款应用中,任何一个系统或者平台上使用都非常频繁,这里介绍一下鸿蒙系统中对话框的用法,分别为:普通文本对话框,自定义提示对话框,对话框菜单,警告提示对话框,列表选择对话框,自定义对话框,日期对话框,时间对话框,文本列表对话框……

1.普通文本对话框

  1. @Entry
  2. @Component
  3. struct Index {
  4. @State message: string = 'Hello World'
  5. //文本提示对话框
  6. build() {
  7. Scroll(){
  8. Column() {
  9. Button("普通文本提示对话框").width("60%").height(60).margin(10)
  10. .onClick(()=>{
  11. promptAction.showToast({
  12. message:this.message,//显示的提示内容
  13. duration:2000,//显示的时长
  14. bottom:200,//设置提示对话框距离底部的间距
  15. })
  16. })
  17. }.width("100%")
  18. }
  19. }
  20. }

2.自定义提示对话框

  1. //该提示对话框默认为中央显示
  2. Button("自定义提示对话框").width("60%").height(60).margin(10)
  3. .onClick(()=>{
  4. promptAction.showDialog({
  5. title:"带按钮的提示对话框",
  6. message:"你好,我是自定义提示对话框",
  7. //对话框中显示两个按钮时横向显示,三个按钮会垂直显示
  8. buttons:[{
  9. text:"按钮一",
  10. color:"#f00",
  11. },
  12. {
  13. text:"按钮二",
  14. color:"#0f0",
  15. },
  16. {
  17. text:"按钮三",
  18. color:"#00f",
  19. }
  20. ]
  21. })
  22. //给按钮添加点击事件
  23. .then(data=>{
  24. switch (data.index){
  25. case 0:
  26. console.info("第一个按钮被点击了")
  27. break;
  28. case 1:
  29. console.info("第二个按钮被点击了")
  30. break;
  31. case 2:
  32. console.info("第三个按钮被点击了")
  33. break;
  34. }
  35. })
  36. })

3.对话框菜单

  1. Button("对话框菜单").width("60%").height(60).margin(10)
  2. .onClick(()=>{
  3. //菜单对话框,最多可添加6个按钮
  4. promptAction.showActionMenu({
  5. title:"对话框菜单",
  6. buttons:[{
  7. text:"按钮一",
  8. color:"#0ff",
  9. },
  10. {
  11. text:"按钮二",
  12. color:"#f0f",
  13. },
  14. {
  15. text:"按钮三",
  16. color:"#f30",
  17. },
  18. {
  19. text:"按钮四",
  20. color:"#aaf",
  21. },
  22. {
  23. text:"按钮五",
  24. color:"#777",
  25. },
  26. {
  27. text:"按钮六",
  28. color:"#0f7",
  29. },]
  30. }).then(data=>{
  31. console.info("第"+(data.index+1)+"个按钮被点击了")
  32. })
  33. })

4.警告提示对话框

  1. Button("警告提示对话框").width("60%").height(60).margin(10)
  2. .onClick(()=>{
  3. AlertDialog.show({
  4. title:"标题",//设置标题
  5. message:"内容",//设置内容
  6. autoCancel:true,//是否允许自动取消
  7. alignment:DialogAlignment.Bottom,//设置对话框显示的位置
  8. offset:{dx:0,dy:-20},//在对齐方式的条件下,设置水平和垂直方向的偏移量
  9. //x轴向右为正,y轴向下为正
  10. gridCount:3,//通过设置占用的栅格数设置对话框的宽度
  11. confirm:{//添加确定按钮
  12. value:"确定",
  13. action:()=>{
  14. console.info("确定按钮被点击了")
  15. },
  16. backgroundColor:"#0ff",//添加按钮的背景颜色
  17. fontColor:"#f0f",//字体颜色
  18. },
  19. cancel:()=>{//当对话框被取消的时候触发
  20. console.info("对话框被取消了")
  21. }
  22. })
  23. })

  1. //警告提示对话框2
  2. Button("警告提示对话框2").width("60%").height(60).margin(10)
  3. .onClick(()=>{
  4. AlertDialog.show({
  5. title:"标题",//设置标题
  6. message:"内容",//设置内容
  7. autoCancel:true,//是否允许自动取消
  8. alignment:DialogAlignment.Bottom,//设置对话框显示的位置
  9. offset:{dx:0,dy:-20},//在对齐方式的条件下,设置水平和垂直方向的偏移量
  10. //x轴向右为正,y轴向下为正
  11. gridCount:3,//通过设置占用的栅格数设置对话框的宽度
  12. primaryButton:{
  13. value:"取消",
  14. action:()=>{
  15. console.info("取消按钮被点击了")
  16. },
  17. },
  18. secondaryButton:{
  19. value:"确定",
  20. action:()=>{
  21. console.info("确定按钮被点击了")
  22. }
  23. },
  24. cancel:()=>{//当对话框被取消的时候触发
  25. console.info("对话框被取消了")
  26. }
  27. })
  28. })

5.列表选择对话框

  1. //列表选择对话框
  2. Button("列表选择对话框").width("60%").height(60).margin(10)
  3. .onClick(()=>{
  4. ActionSheet.show({
  5. title:"标题",
  6. message:"内容",
  7. autoCancel:true,
  8. alignment:DialogAlignment.Bottom,
  9. offset:{dx:0,dy:-20},
  10. sheets:[{
  11. title:"苹果",
  12. icon:$r("app.media.icon"),
  13. action:()=>{
  14. console.info("苹果选项被点击了")
  15. }
  16. },
  17. {
  18. title:"香蕉",
  19. icon:$r("app.media.icon"),
  20. action:()=>{
  21. console.info("香蕉选项被点击了")
  22. }
  23. },
  24. {
  25. title:"西瓜",
  26. icon:$r("app.media.icon"),
  27. action:()=>{
  28. console.info("西瓜选项被点击了")
  29. }
  30. },],
  31. confirm:{
  32. value:"确定",
  33. action:()=>{
  34. console.info("确定按钮被点击了")
  35. }
  36. },
  37. cancel:()=>{
  38. console.info("对话框被取消了")
  39. }
  40. })
  41. })

6.自定义对话框

  1. import promptAction from '@ohos.promptAction'
  2. //自定义对话框
  3. @CustomDialog
  4. struct Dialog1{
  5. //自定义对话框控制器
  6. controller:CustomDialogController
  7. cancel:()=>void //定义对话框被取消的事件
  8. confirm:()=>void //定义对话框被确定的事件
  9. build(){
  10. Column(){
  11. Text("标题").width("100%").height(60).backgroundColor("#600f")
  12. .fontColor(Color.White).fontWeight(FontWeight.Bold).fontSize(26)
  13. .textAlign(TextAlign.Center).letterSpacing(10)
  14. Text("我是自定义对话框的内容").width("100%").height(60).margin({left:10})
  15. .fontSize(24).fontColor(Color.Black)
  16. Row({space:10}){
  17. Button("取消").width("40%").backgroundColor(Color.Red)
  18. .fontColor(Color.White)
  19. .height(40).type(ButtonType.Normal).borderRadius(10)
  20. .onClick(()=>{
  21. this.controller.close()
  22. this.cancel()
  23. })
  24. Button("确定").width("40%").backgroundColor("#00f")
  25. .fontColor(Color.White)
  26. .height(40).type(ButtonType.Normal).borderRadius(10)
  27. .onClick(()=>{
  28. this.controller.close()
  29. this.confirm()
  30. })
  31. }
  32. }.width("100%").margin({bottom:10})
  33. }
  34. }
  35. @Entry
  36. @Component
  37. struct Index {
  38. //初始化自定义对话框的控制器
  39. dialogController:CustomDialogController=new CustomDialogController({
  40. //创建自定义对话框对象
  41. builder:Dialog1({
  42. //实现两个未实现的方法
  43. cancel:this.onCancel,
  44. confirm:this.onAccept,
  45. }),
  46. autoCancel:true,
  47. alignment:DialogAlignment.Bottom,
  48. })
  49. //定义两个方法
  50. onCancel(){
  51. console.info("自定义对话框被取消了")
  52. }
  53. onAccept(){
  54. console.info("自定义对话框被确定了")
  55. }
  56. //文本提示对话框
  57. build() {
  58. Scroll(){
  59. Column() {
  60. Button("自定义对话框").width("60%").height(60).margin(10)
  61. .onClick(()=>{
  62. //通过控制器打开自定义对话框 关闭调用close方法
  63. this.dialogController.open()
  64. })
  65. }
  66. .width('100%')
  67. }
  68. }
  69. }

7.日期对话框

  1. //日期对话框
  2. Button("日期对话框").width("60%").height(60).margin(10)
  3. .onClick(()=>{
  4. //创建日期对话框
  5. DatePickerDialog.show({
  6. start:new Date("2000-1-1"), //设置开始的日期
  7. end:new Date("2100-1-1"),//设置结束的日期
  8. selected:this.currentDate,//设置当前选中的日期为现在的日期
  9. // lunar:true,//允许月份和天数显示为中文
  10. onAccept:(value:DatePickerResult)=>{
  11. //保存当前这一次用户选中的日期,用户下一次打开对话框时,显示上一次选中的日期
  12. this.currentDate.setFullYear(value.year,value.month,value.day)
  13. console.info(JSON.stringify(value))
  14. },
  15. onCancel:()=>{
  16. console.info("日期对话框被取消了")
  17. },
  18. onChange:(value:DatePickerResult)=>{
  19. console.info("用户正在更改日期"+JSON.stringify(value))
  20. }
  21. })
  22. })

8.时间对话框

  1. //时间对话框
  2. Button("时间对话框").width("60%").height(60).margin(10)
  3. .onClick(()=>{
  4. TimePickerDialog.show({
  5. //设置当前的时间
  6. selected:this.currentDate,
  7. useMilitaryTime:true,//是否使用24小时制
  8. //选中事件
  9. onAccept:(value:TimePickerResult)=>{
  10. this.currentDate.setHours(value.hour,value.minute)
  11. console.info(JSON.stringify(value))
  12. },
  13. //取消事件
  14. onCancel:()=>{
  15. console.info("时间对话框被取消了")
  16. },
  17. //改变事件
  18. onChange:(value:TimePickerResult)=>{
  19. console.info(JSON.stringify(value))
  20. },
  21. })
  22. })

9.文本列表对话框

  1. //文本列表对话框
  2. Button("文本列表对话框").width("60%").height(60).margin(10)
  3. .onClick(()=>{
  4. TextPickerDialog.show({
  5. range:this.texts,//添加数据信息
  6. selected:2,//默认选中第几项,默认第0项开始
  7. onAccept:(value:TextPickerResult)=>{
  8. promptAction.showToast({message:"下标为:"+value.index+"的"+value.value+"被选中了"})
  9. },
  10. onCancel:()=>{
  11. promptAction.showToast({message:"对话框取消了"})
  12. },
  13. onChange:(value:TextPickerResult)=>{
  14. console.info(JSON.stringify(value))
  15. }
  16. })
  17. })

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/887946
推荐阅读
相关标签
  

闽ICP备14008679号