鸿蒙HarmonyOS三方件开发指南(20)-Dialog组件

开发 前端 OpenHarmony
Dialog组件是一个显示不同风格的自定义对话框组件,目前支持十一种风格的显示。

[[407820]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

HarmonyOS三方件开发指南自一月份上线以来,已经连续更新了十九期了。今天所发这篇文章将作为三方件开发指南第一期的收官之作。我庆幸社区有这么多志同道合的开发者们关注我的文章,关于三方件的讲解或许对你有所帮助,或许你有更好的建议或想法,都可以来告诉我们。

HarmonyOS的未来是强大的,因为我们广大开发者是强大的,是自信的,我也祝福各位开发者们立“鸿”鹄之志,逐梦未来!

Dialog组件功能介绍

功能介绍

Dialog组件是一个显示不同风格的自定义对话框组件,目前支持十一种风格的显示。

模拟器上运行效果

【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区
【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区
【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区
【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区
【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区
【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区
【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区
【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区
【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区
【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区
【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区

Dialog使用方法

新建工程,增加组件Har包依赖

在应用模块中添加HAR,只需要将dialoglibrary-debug.har复制到entry\libs目录下即可(由于build.gradle中已经依赖的libs目录下的*.har,因此不需要在做修改)。

修改配置文件

在entry下面的build.gradle添加library 的依赖

【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区

在代码中使用

  1.   configBean = StyledDialog.buildMdAlert(getContext(), "提示""确定退出?", new MyDialogListener() { 
  2.     @Override 
  3.     public void positiveButton() { 
  4.         configBean.commonDialog.hide(); 
  5.         configBean.commonDialog = null
  6.     } 
  7.  
  8.     @Override 
  9.     public void negativeButton() { 
  10.         configBean.commonDialog.hide(); 
  11.         configBean.commonDialog = null
  12.     } 
  13. }); 
  14. configBean.commonDialog.show(); 

 show input dialog

  1. configBean = StyledDialog.buildNormalInput(getContext(), "请输入您的昵称""长度0-20", new MyDialogListener() { 
  2.  
  3.     @Override 
  4.     public void positiveButton() { 
  5.         //可对输入内容进行校验 
  6.         configBean.getInputText1(); 
  7.         configBean.commonDialog.hide(); 
  8.         configBean.commonDialog = null
  9.     } 
  10.  
  11.     @Override 
  12.     public void negativeButton() { 
  13.         configBean.commonDialog.hide(); 
  14.         configBean.commonDialog = null
  15.     } 
  16. }); 
  17. configBean.commonDialog.show(); 

show single choose dialog

  1. CharSequence[] words = {"身份证""手机""钥匙""钱包"}; 
  2. boolean[] selectItems = {truefalsefalsefalse}; 
  3.  
  4. ArrayList<ChooseBean> list = new ArrayList<>(); 
  5. for (int i = 0; i < words.length; i++) { 
  6.     ChooseBean chooseBean = new ChooseBean(); 
  7.     chooseBean.setTxt(words[i]); 
  8.     chooseBean.setChoosen(selectItems[i]); 
  9.     list.add(chooseBean); 
  10. configBean = StyledDialog.buildMdSingleChoose(getContext(), "单选", list, new MyItemDialogListener() { 
  11.     @Override 
  12.     public void onItemClick(CharSequence text, int position) { 
  13.         new ToastDialog(getContext()).setText(text.toString()).show(); 
  14.  
  15.     } 
  16.  
  17.     @Override 
  18.     public void positiveButton() { 
  19.         configBean.commonDialog.hide(); 
  20.         configBean.commonDialog = null
  21.  
  22.     } 
  23.  
  24.     @Override 
  25.     public void negativeButton() { 
  26.         configBean.commonDialog.hide(); 
  27.         configBean.commonDialog = null
  28.     } 
  29. }); 
  30. configBean.commonDialog.show(); 

show multi choose dialog

  1. CharSequence[] words = {"A.apple""B.bananer""C.pear""D.milk"}; 
  2. boolean[] checkedItems = {falsefalsefalsefalse}; 
  3.  
  4. ArrayList<ChooseBean> list = new ArrayList<>(); 
  5. for (int i = 0; i < words.length; i++) { 
  6.     ChooseBean chooseBean = new ChooseBean(); 
  7.     chooseBean.setTxt(words[i]); 
  8.     chooseBean.setChoosen(checkedItems[i]); 
  9.     list.add(chooseBean); 
  10. configBean = StyledDialog.buildMdMultiChoose(getContext(), "多选", list, 
  11.         new MyCheckBoxItemDialogListener() { 
  12.     @Override 
  13.     public void onItemClick(Checkbox checkbox, int position) { 
  14.         checkedItems[position] = checkbox.isChecked(); 
  15.     } 
  16.  
  17.     @Override 
  18.     public void positiveButton() { 
  19.         configBean.commonDialog.hide(); 
  20.         configBean.commonDialog = null
  21.  
  22.     } 
  23.  
  24.     @Override 
  25.     public void negativeButton() { 
  26.         configBean.commonDialog.hide(); 
  27.         configBean.commonDialog = null
  28.     } 
  29. }); 
  30. configBean.commonDialog.show(); 

show ios alert dialog

  1. configBean = StyledDialog.buildMdIOSAlert(getContext(), "提示""确定退出?", new MyDialogListener() { 
  2.     @Override 
  3.     public void positiveButton() { 
  4.         configBean.commonDialog.hide(); 
  5.         configBean.commonDialog = null
  6.  
  7.     } 
  8.  
  9.     @Override 
  10.     public void negativeButton() { 
  11.         configBean.commonDialog.hide(); 
  12.         configBean.commonDialog = null
  13.     } 
  14. }); 
  15.  
  16.  
  17. configBean.commonDialog.show(); 

show ios vertical alert dialog

  1. configBean = StyledDialog.buildMdIOSAlertVertical(getContext(), "提示""确定退出?", new MyDialogListener() { 
  2.     @Override 
  3.     public void positiveButton() { 
  4.         configBean.commonDialog.hide(); 
  5.         configBean.commonDialog = null
  6.  
  7.     } 
  8.  
  9.     @Override 
  10.     public void negativeButton() { 
  11.         configBean.commonDialog.hide(); 
  12.         configBean.commonDialog = null
  13.     } 
  14. }); 
  15.  
  16.  
  17. configBean.commonDialog.show(); 

show two input dialog

  1. configBean = StyledDialog.buildTwoInput(getContext(), "登录""请输入用户名","请输入密码", new MyDialogListener() { 
  2.  
  3.     @Override 
  4.     public void positiveButton() { 
  5.         //可对输入内容进行校验 
  6.         configBean.getInputText1(); 
  7.         configBean.getInputText2(); 
  8.         configBean.commonDialog.hide(); 
  9.         configBean.commonDialog = null
  10.     } 
  11.  
  12.     @Override 
  13.     public void negativeButton() { 
  14.         configBean.commonDialog.hide(); 
  15.         configBean.commonDialog = null
  16.     } 
  17. }); 
  18. configBean.commonDialog.show(); 

 show list dialog

  1. CharSequence[] words = {"身份证""手机""钥匙""钱包"}; 
  2. boolean[] selectItems = {truefalsefalsefalse}; 
  3.  
  4. ArrayList<ChooseBean> list = new ArrayList<>(); 
  5. for (int i = 0; i < words.length; i++) { 
  6.     ChooseBean chooseBean = new ChooseBean(); 
  7.     chooseBean.setTxt(words[i]); 
  8.     chooseBean.setChoosen(selectItems[i]); 
  9.     list.add(chooseBean); 
  10. configBean = StyledDialog.buildMdIOSSingleChoose(getContext(), list, new MyItemDialogListener() { 
  11.     @Override 
  12.     public void onItemClick(CharSequence text, int position) { 
  13.         new ToastDialog(getContext()).setText(text.toString()).show(); 
  14.  
  15.     } 
  16.  
  17.     @Override 
  18.     public void positiveButton() { 
  19.         configBean.commonDialog.hide(); 
  20.         configBean.commonDialog = null
  21.  
  22.     } 
  23.  
  24.     @Override 
  25.     public void negativeButton() { 
  26.         configBean.commonDialog.hide(); 
  27.         configBean.commonDialog = null
  28.     } 
  29. }); 
  30. configBean.commonDialog.show(); 

show bottom button list dialog

  1. CharSequence[] words = {"身份证""手机""钥匙""钱包"}; 
  2. boolean[] selectItems = {truefalsefalsefalse}; 
  3.  
  4. ArrayList<ChooseBean> list = new ArrayList<>(); 
  5. for (int i = 0; i < words.length; i++) { 
  6.     ChooseBean chooseBean = new ChooseBean(); 
  7.     chooseBean.setTxt(words[i]); 
  8.     chooseBean.setChoosen(selectItems[i]); 
  9.     list.add(chooseBean); 
  10. configBean = StyledDialog.buildMdIOSBottomSingleChoose(getContext(), list, new MyItemDialogListener() { 
  11.     @Override 
  12.     public void onItemClick(CharSequence text, int position) { 
  13.         new ToastDialog(getContext()).setText(text.toString()).show(); 
  14.  
  15.     } 
  16.  
  17.     @Override 
  18.     public void positiveButton() { 
  19.         configBean.commonDialog.hide(); 
  20.         configBean.commonDialog = null
  21.  
  22.     } 
  23.  
  24.     @Override 
  25.     public void negativeButton() { 
  26.         configBean.commonDialog.hide(); 
  27.         configBean.commonDialog = null
  28.     } 
  29. }); 
  30. configBean.commonDialog.show(); 

show bottom list dialog

  1. CharSequence[] words = {"身份证""手机""钥匙""钱包"}; 
  2. boolean[] selectItems = {truefalsefalsefalse}; 
  3.  
  4. ArrayList<ChooseBean> list = new ArrayList<>(); 
  5. for (int i = 0; i < words.length; i++) { 
  6.     ChooseBean chooseBean = new ChooseBean(); 
  7.     chooseBean.setTxt(words[i]); 
  8.     chooseBean.setChoosen(selectItems[i]); 
  9.     list.add(chooseBean); 
  10. configBean = StyledDialog.buildMdIOSList(getContext(), list, new MyItemDialogListener() { 
  11.     @Override 
  12.     public void onItemClick(CharSequence text, int position) { 
  13.         new ToastDialog(getContext()).setText(text.toString()).show(); 
  14.  
  15.     } 
  16.  
  17.     @Override 
  18.     public void positiveButton() { 
  19.         configBean.commonDialog.hide(); 
  20.         configBean.commonDialog = null
  21.  
  22.     } 
  23.  
  24.     @Override 
  25.     public void negativeButton() { 
  26.         configBean.commonDialog.hide(); 
  27.         configBean.commonDialog = null
  28.     } 
  29. }); 
  30. configBean.commonDialog.show(); 

Menudialog

  1. configBean = StyledDialog.buildMdSingleMenuChoose(getContext(), list, 100, 100, 400,650,new MyItemDialogListener() { 
  2.                 @Override 
  3.                 public void onItemClick(CharSequence text, int position) { 
  4. //                    new ToastDialog(getContext()).setText(text.toString()).show(); 
  5.  
  6.                 } 
  7.  
  8.                 @Override 
  9.                 public void positiveButton() { 
  10.                     configBean.commonDialog.hide(); 
  11.                     configBean.commonDialog = null
  12.  
  13.                 } 
  14.  
  15.                 @Override 
  16.                 public void negativeButton() { 
  17.                     configBean.commonDialog.hide(); 
  18.                     configBean.commonDialog = null
  19.                 } 
  20.             }); 
  21.             configBean.commonDialog.show(); 

Dialog开发实现

新建一个Module

新建一个Module,类型选择HarmonyOS Library,模块名为dialoglibrary,如图

【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区

实现类

新建一个StyleDialog类,添加静态方法,代码如下:

  1. /** 
  2.  * @param context  context 
  3.  * @param title    对话框的title 
  4.  * @param msg      对话框的提示信息 
  5.  * @param listener 对话框 ok 和 cancle 的点击监听事件 
  6.  * @return configbean 
  7.  */ 
  8. public static ConfigBean buildMdAlert(Context context, CharSequence title, CharSequence msg, 
  9.                                       MyDialogListener listener) { 
  10.     return DialogAssigner.getInstance().assignMdAlert(context, title, msg, listener); 
  11.  
  12. /** 
  13.  * @param context  context 
  14.  * @param title    对话框的title 
  15.  * @param list     list 
  16.  * @param listener 单项的item 和ok cancle 的点击事件的监听 
  17.  * @return configbean 
  18.  */ 
  19. public static ConfigBean buildMdSingleChoose(Context context, CharSequence title, ArrayList<ChooseBean> list, 
  20.                                              MyItemDialogListener listener) { 
  21.     return DialogAssigner.getInstance().assignMdSingleChoose(context, title, list, listener); 
  22.  
  23. /** 
  24.  * @param context     context 
  25.  * @param title       对话框的title 
  26.  * @param list        list 
  27.  * @param btnListener ok cancle 的点击监听事件 
  28.  * @return configbean 
  29.  */ 
  30. public static ConfigBean buildMdMultiChoose(Context context, CharSequence title, ArrayList<ChooseBean> list, 
  31.                                             MyCheckBoxItemDialogListener btnListener) { 
  32.     return DialogAssigner.getInstance().assignMdMultiChoose(context, title, list, btnListener); 
  33.  
  34. /** 
  35.  * @param context  context 
  36.  * @param title    对话框的title 
  37.  * @param hint1    输入框的hint 文字提示信息 
  38.  * @param listener ok cancle 的点击监听事件 
  39.  * @return configbean 
  40.  */ 
  41. public static ConfigBean buildNormalInput(Context context, CharSequence title, CharSequence hint1, 
  42.                                           MyDialogListener listener) { 
  43.     return DialogAssigner.getInstance().assignNormalInput(context, title, hint1, listener); 

新建DialogAssigner implements Assignable,代码如下:

  1. /** 
  2.  * 
  3.  */ 
  4. private static DialogAssigner instance; 
  5.  
  6. /** 
  7.  * 
  8.  */ 
  9. private DialogAssigner() { 
  10.  
  11.  
  12. /** 
  13.  * 
  14.  */ 
  15. public static DialogAssigner getInstance() { 
  16.     if (instance == null) { 
  17.         instance = new DialogAssigner(); 
  18.     } 
  19.     return instance; 
  20.  
  21.  
  22. @Override 
  23. public ConfigBean assignMdAlert(Context context, CharSequence title, CharSequence msg, MyDialogListener listener) { 
  24.  
  25.     ConfigBean bean = new ConfigBean(); 
  26.     bean.context = context; 
  27.  
  28.     bean.msg = msg; 
  29.     bean.title = title; 
  30.     bean.listener = listener; 
  31.     bean.type = DefaultConfig.ALERT_DIALOG; 
  32.     bean.buildByType(bean); 
  33.     return bean; 
  34.  
  35. @Override 
  36. public ConfigBean assignNormalInput(Context context, CharSequence title, CharSequence hint1, 
  37.                                     MyDialogListener listener) { 
  38.     ConfigBean bean = new ConfigBean(); 
  39.     bean.context = context; 
  40.     bean.listener = listener; 
  41.     bean.title = title; 
  42.     bean.hint1 = hint1; 
  43.     bean.type = DefaultConfig.INPUT_DIALOG; 
  44.     bean.buildByType(bean); 
  45.     return bean; 
  46.  
  47. @Override 
  48. public ConfigBean assignMdSingleChoose(Context context, CharSequence title, ArrayList<ChooseBean> list, 
  49.                                        MyItemDialogListener listener) { 
  50.     ConfigBean bean = new ConfigBean(); 
  51.     bean.context = context; 
  52.     bean.title = title; 
  53.     bean.itemListener = listener; 
  54.     bean.type = DefaultConfig.SINGLE_DIALOG; 
  55.     bean.chooseBeans.addAll(list); 
  56.     bean.buildByType(bean); 
  57.     return bean; 
  58.  
  59. @Override 
  60. public ConfigBean assignMdMultiChoose(Context context, CharSequence title, ArrayList<ChooseBean> list, 
  61.                                       MyCheckBoxItemDialogListener checkboxListener) { 
  62.     ConfigBean bean = new ConfigBean(); 
  63.     bean.context = context; 
  64.     bean.msg = title; 
  65.     bean.title = title; 
  66.     bean.checkBoxItemDialogListener = checkboxListener; 
  67.     bean.type = DefaultConfig.MULTI_DIALOG; 
  68.     bean.chooseBeans.addAll(list); 
  69.     bean.buildByType(bean); 
  70.  
  71.     return bean; 

新建MyDialogBuilder,实现不同dialog的生成

  1. /** 
  2.  * 根据dialog类型,生成不同类型的dialog 
  3.  * 
  4.  * @param bean bean 
  5.  * @return configbean 
  6.  */ 
  7. public ConfigBean buildByType(ConfigBean bean) { 
  8.  
  9.     switch (bean.type) { 
  10.         case DefaultConfig.ALERT_DIALOG: 
  11.             buildMdAlert(bean); 
  12.  
  13.             break; 
  14.         case DefaultConfig.INPUT_DIALOG: 
  15.             buildMdInput(bean); 
  16.  
  17.             break; 
  18.         case DefaultConfig.SINGLE_DIALOG: 
  19.             buildMdSingleChoose(bean); 
  20.  
  21.             break; 
  22.         case DefaultConfig.MULTI_DIALOG: 
  23.             buildMdMultiChoose(bean); 
  24.             break; 
  25.         default
  26.             break; 
  27.     } 
  28.     return bean; 
  29.  
  30. /** 
  31.  * 生成alert dialog 
  32.  * 
  33.  * @param bean bean 
  34.  * @return ConfigBean 
  35.  */ 
  36. protected ConfigBean buildMdAlert(final ConfigBean bean) { 
  37.     CommonDialog commonDialog = new CommonDialog(bean.context); 
  38.     Component component = LayoutScatter.getInstance(bean.context).parse(ResourceTable.Layout_alert_dialog_layout, 
  39.             nullfalse); 
  40.     commonDialog.setContentCustomComponent(component); 
  41.     commonDialog.setTransparent(true); 
  42.     Text titleTV = (Text) component.findComponentById(ResourceTable.Id_alert_dialog_title); 
  43.     titleTV.setText(bean.title.toString()); 
  44.     Text messageTV = (Text) component.findComponentById(ResourceTable.Id_alert_dialog_message); 
  45.     messageTV.setText(bean.msg.toString()); 
  46.     Button ok = (Button) component.findComponentById(ResourceTable.Id_alert_dialog_ok); 
  47.     ok.setClickedListener(component1 -> { 
  48.         bean.listener.positiveButton(); 
  49.     }); 
  50.     Button cancle = (Button) component.findComponentById(ResourceTable.Id_alert_dialog_cancle); 
  51.     cancle.setClickedListener(component1 -> { 
  52.         bean.listener.negativeButton(); 
  53.     }); 
  54.     bean.commonDialog = commonDialog; 
  55.     return bean; 
  56.  
  57. /** 
  58.  * 生成input dialog 
  59.  * 
  60.  * @param bean bean 
  61.  * @return ConfigBean 
  62.  */ 
  63. protected ConfigBean buildMdInput(final ConfigBean bean) { 
  64.  
  65.     CommonDialog commonDialog = new CommonDialog(bean.context); 
  66.     Component component = 
  67.             LayoutScatter.getInstance(bean.context).parse(ResourceTable.Layout_input_dialog_layout, null
  68.                     false); 
  69.     commonDialog.setContentCustomComponent(component); 
  70.     commonDialog.setTransparent(true); 
  71.     Text titleTV = (Text) component.findComponentById(ResourceTable.Id_input_dialog_title); 
  72.     titleTV.setText(bean.title.toString()); 
  73.     TextField messageTV = (TextField) component.findComponentById(ResourceTable.Id_input_dialog_text_field); 
  74.     messageTV.setHint(bean.hint1.toString()); 
  75.     Button ok = (Button) component.findComponentById(ResourceTable.Id_input_dialog_ok2); 
  76.     ok.setClickedListener(component1 -> { 
  77.         bean.listener.positiveButton(); 
  78.     }); 
  79.     Button cancle = (Button) component.findComponentById(ResourceTable.Id_input_dialog_cancle); 
  80.     cancle.setClickedListener(component1 -> { 
  81.         bean.listener.negativeButton(); 
  82.     }); 
  83.     bean.commonDialog = commonDialog; 
  84.     return bean; 
  85.  
  86. /** 
  87.  * 生成单选dialog 
  88.  * 
  89.  * @param bean bean 
  90.  * @return ConfigBean 
  91.  */ 
  92. protected ConfigBean buildMdSingleChoose(final ConfigBean bean) { 
  93.     CommonDialog commonDialog = new CommonDialog(bean.context); 
  94.     Component component = 
  95.             LayoutScatter.getInstance(bean.context).parse(ResourceTable.Layout_single_choose_dialog_layout, null
  96.                     false); 
  97.     commonDialog.setTransparent(true); 
  98.     Text titleTv = (Text) component.findComponentById(ResourceTable.Id_single_dialog_title); 
  99.     titleTv.setText(bean.title.toString()); 
  100.     RadioContainer radioContainer = (RadioContainer) component.findComponentById(ResourceTable.Id_radio_container); 
  101.     Resource selectResource = null
  102.     Resource emptyResource = null
  103.     try { 
  104.         selectResource = bean.context.getResourceManager().getResource(ResourceTable.Media_select); 
  105.         emptyResource = bean.context.getResourceManager().getResource(ResourceTable.Media_unselect); 
  106.     } catch (IOException e) { 
  107.         e.printStackTrace(); 
  108.     } catch (NotExistException e) { 
  109.         e.printStackTrace(); 
  110.     } 
  111.  
  112.     PixelMapElement selectElement = new PixelMapElement(selectResource); 
  113.     PixelMapElement emptyElement = new PixelMapElement(emptyResource); 
  114.  
  115.     for (int i = 0; i < bean.chooseBeans.size(); i++) { 
  116.         ChooseBean chooseBean = bean.chooseBeans.get(i); 
  117.         RadioButton radioButton = new RadioButton(bean.context); 
  118.         radioButton.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT); 
  119.         radioButton.setHeight(150); 
  120.         radioButton.setTextSize(50); 
  121.         radioButton.setText(chooseBean.getTxt().toString()); 
  122.         radioButton.setButtonElement(null); 
  123.         if (chooseBean.isChoosen()) { 
  124.             radioButton.setSelected(true); 
  125.         } else { 
  126.             radioButton.setSelected(false); 
  127.         } 
  128.         StateElement checkElement = new StateElement(); 
  129.         checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_SELECTED}, selectElement); 
  130.         checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, emptyElement); 
  131.         radioButton.setAroundElements(checkElement, nullnullnull); 
  132.         radioContainer.addComponent(radioButton); 
  133.     } 
  134.     radioContainer.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() { 
  135.         @Override 
  136.         public void onCheckedChanged(RadioContainer r, int i) { 
  137.             for (int j = 0; j < bean.chooseBeans.size(); j++) { 
  138.                 RadioButton radioButton1 = (RadioButton) r.getComponentAt(j); 
  139.                 if (j == i) { 
  140.                     radioButton1.setSelected(true); 
  141.                     bean.itemListener.onItemClick(bean.chooseBeans.get(i).getTxt(), i); 
  142.                 } else { 
  143.                     radioButton1.setSelected(false); 
  144.                 } 
  145.             } 
  146.         } 
  147.     }); 
  148.  
  149.     Button ok = (Button) component.findComponentById(ResourceTable.Id_single_dialog_ok); 
  150.     ok.setClickedListener(component1 -> { 
  151.         bean.itemListener.positiveButton(); 
  152.     }); 
  153.     Button cancle = (Button) component.findComponentById(ResourceTable.Id_single_dialog_cancle); 
  154.     cancle.setClickedListener(component1 -> { 
  155.         bean.itemListener.negativeButton(); 
  156.     }); 
  157.  
  158.     component.invalidate(); 
  159.     commonDialog.setContentCustomComponent(component); 
  160.     bean.commonDialog = commonDialog; 
  161.  
  162.  
  163.     return bean; 
  164.  
  165. /** 
  166.  * 生成多选dialog 
  167.  * 
  168.  * @param bean bean 
  169.  * @return ConfigBean 
  170.  */ 
  171. protected ConfigBean buildMdMultiChoose(final ConfigBean bean) { 
  172.     CommonDialog commonDialog = new CommonDialog(bean.context); 
  173.     Component component = 
  174.             LayoutScatter.getInstance(bean.context).parse(ResourceTable.Layout_multi_choose_dialog_layout, null
  175.                     false); 
  176.  
  177.     commonDialog.setTransparent(true); 
  178.     DirectionalLayout layout = (DirectionalLayout) component.findComponentById(ResourceTable.Id_checkbox_layout); 
  179.     Resource selectResource = null
  180.     Resource emptyResource = null
  181.     try { 
  182.         selectResource = bean.context.getResourceManager().getResource(ResourceTable.Media_checkbox_select); 
  183.         emptyResource = bean.context.getResourceManager().getResource(ResourceTable.Media_checkbox_unselect); 
  184.     } catch (IOException e) { 
  185.         e.printStackTrace(); 
  186.     } catch (NotExistException e) { 
  187.         e.printStackTrace(); 
  188.     } 
  189.  
  190.     PixelMapElement selectElement = new PixelMapElement(selectResource); 
  191.     PixelMapElement emptyElement = new PixelMapElement(emptyResource); 
  192.  
  193.     for (int i = 0; i < bean.chooseBeans.size(); i++) { 
  194.         ChooseBean chooseBean = bean.chooseBeans.get(i); 
  195.         Checkbox checkbox = new Checkbox(bean.context); 
  196.         checkbox.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT); 
  197.         checkbox.setHeight(150); 
  198.         checkbox.setTextSize(50); 
  199.         checkbox.setText(chooseBean.getTxt().toString()); 
  200.  
  201.         if (chooseBean.isChoosen()) { 
  202.             checkbox.setChecked(true); 
  203.  
  204.         } else { 
  205.             checkbox.setChecked(false); 
  206.         } 
  207.  
  208.         checkbox.setButtonElement(null); 
  209.         checkbox.setTag(i); 
  210.  
  211.         StateElement checkElement = new StateElement(); 
  212.         checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, selectElement); 
  213.         checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, emptyElement); 
  214.         checkbox.setAroundElements(checkElement, nullnullnull); 
  215.         checkbox.setCheckedStateChangedListener((absButton, state) -> { 
  216.             absButton.setChecked(state); 
  217.  
  218.             absButton.setAroundElements(checkElement, nullnullnull); 
  219.             bean.checkBoxItemDialogListener.onItemClick((Checkbox) absButton, (int) absButton.getTag()); 
  220.         }); 
  221.  
  222.         layout.addComponent(checkbox); 
  223.     } 
  224.  
  225.     Button ok = (Button) component.findComponentById(ResourceTable.Id_multi_dialog_ok); 
  226.     ok.setClickedListener(component1 -> { 
  227.         bean.checkBoxItemDialogListener.positiveButton(); 
  228.     }); 
  229.     Button cancle = (Button) component.findComponentById(ResourceTable.Id_multi_dialog_cancle); 
  230.     cancle.setClickedListener(component1 -> { 
  231.         bean.checkBoxItemDialogListener.negativeButton(); 
  232.     }); 
  233.     component.invalidate(); 
  234.     commonDialog.setContentCustomComponent(component); 
  235.     bean.commonDialog = commonDialog; 
  236.     return bean; 

新建监听接口MyDialogListener、MyItemDialogListener、MyCheckBoxItemDialogListener

编译HAR包

利用Gradle可以将HarmonyOS Library库模块构建为HAR包,构建HAR包的方法如下:

在Gradle构建任务中,双击PackageDebugHar或PackageReleaseHar任务,构建Debug类型或Release类型的HAR。

待构建任务完成后,可以在工程目录中的styledialog> bulid > outputs > har目录中,获取生成的HAR包。

【软通动力】HarmonyOS三方件开发指南(20)-Dialog组件-鸿蒙HarmonyOS技术社区

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-01-12 12:04:40

鸿蒙HarmonyOS应用开发

2021-01-20 09:54:56

鸿蒙HarmonyOS开发

2021-01-21 13:21:18

鸿蒙HarmonyOSPhotoview组件

2021-01-18 09:52:20

鸿蒙HarmonyOS开发

2021-02-04 09:45:19

鸿蒙HarmonyOS应用开发

2021-02-24 15:22:47

鸿蒙HarmonyOS应用开发

2021-03-01 09:48:24

鸿蒙HarmonyOS应用开发

2021-01-22 17:33:03

鸿蒙HarmonyOS应用开发

2021-01-13 09:40:31

鸿蒙HarmonyOS开发

2021-02-04 13:06:38

鸿蒙HarmonyOS应用开发

2021-04-16 09:28:18

鸿蒙HarmonyOS应用

2021-05-12 15:17:39

鸿蒙HarmonyOS应用

2021-02-26 14:15:27

鸿蒙HarmonyOS应用开发

2021-03-01 14:01:41

鸿蒙HarmonyOS应用开发

2021-03-19 17:42:01

鸿蒙HarmonyOS应用开发

2021-04-20 09:42:20

鸿蒙HarmonyOS应用开发

2021-04-12 09:36:54

鸿蒙HarmonyOS应用

2021-03-31 09:50:25

鸿蒙HarmonyOS应用开发

2021-03-10 15:03:40

鸿蒙HarmonyOS应用

2021-08-26 16:07:46

鸿蒙HarmonyOS应用
点赞
收藏

51CTO技术栈公众号