自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

鴻蒙HarmonyOS三方件開發(fā)指南(20)-Dialog組件

開發(fā) 前端 OpenHarmony
Dialog組件是一個(gè)顯示不同風(fēng)格的自定義對(duì)話框組件,目前支持十一種風(fēng)格的顯示。

[[407820]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

HarmonyOS三方件開發(fā)指南自一月份上線以來,已經(jīng)連續(xù)更新了十九期了。今天所發(fā)這篇文章將作為三方件開發(fā)指南第一期的收官之作。我慶幸社區(qū)有這么多志同道合的開發(fā)者們關(guān)注我的文章,關(guān)于三方件的講解或許對(duì)你有所幫助,或許你有更好的建議或想法,都可以來告訴我們。

HarmonyOS的未來是強(qiáng)大的,因?yàn)槲覀儚V大開發(fā)者是強(qiáng)大的,是自信的,我也祝福各位開發(fā)者們立“鴻”鵠之志,逐夢(mèng)未來!

Dialog組件功能介紹

功能介紹

Dialog組件是一個(gè)顯示不同風(fēng)格的自定義對(duì)話框組件,目前支持十一種風(fēng)格的顯示。

模擬器上運(yùn)行效果

【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)
【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)
【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)
【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)
【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)
【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)
【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)
【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)
【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)
【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)
【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)

Dialog使用方法

新建工程,增加組件Har包依賴

在應(yīng)用模塊中添加HAR,只需要將dialoglibrary-debug.har復(fù)制到entry\libs目錄下即可(由于build.gradle中已經(jīng)依賴的libs目錄下的*.har,因此不需要在做修改)。

修改配置文件

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

【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)

在代碼中使用

  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(), "請(qǐng)輸入您的昵稱""長(zhǎng)度0-20", new MyDialogListener() { 
  2.  
  3.     @Override 
  4.     public void positiveButton() { 
  5.         //可對(duì)輸入內(nèi)容進(jìn)行校驗(yàn) 
  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 = {"身份證""手機(jī)""鑰匙""錢包"}; 
  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(), "登錄""請(qǐng)輸入用戶名","請(qǐng)輸入密碼", new MyDialogListener() { 
  2.  
  3.     @Override 
  4.     public void positiveButton() { 
  5.         //可對(duì)輸入內(nèi)容進(jìn)行校驗(yàn) 
  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 = {"身份證""手機(jī)""鑰匙""錢包"}; 
  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 = {"身份證""手機(jī)""鑰匙""錢包"}; 
  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 = {"身份證""手機(jī)""鑰匙""錢包"}; 
  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開發(fā)實(shí)現(xiàn)

新建一個(gè)Module

新建一個(gè)Module,類型選擇HarmonyOS Library,模塊名為dialoglibrary,如圖

【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)

實(shí)現(xiàn)類

新建一個(gè)StyleDialog類,添加靜態(tài)方法,代碼如下:

  1. /** 
  2.  * @param context  context 
  3.  * @param title    對(duì)話框的title 
  4.  * @param msg      對(duì)話框的提示信息 
  5.  * @param listener 對(duì)話框 ok 和 cancle 的點(diǎn)擊監(jiān)聽事件 
  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    對(duì)話框的title 
  15.  * @param list     list 
  16.  * @param listener 單項(xiàng)的item 和ok cancle 的點(diǎn)擊事件的監(jiān)聽 
  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       對(duì)話框的title 
  26.  * @param list        list 
  27.  * @param btnListener ok cancle 的點(diǎn)擊監(jiān)聽事件 
  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    對(duì)話框的title 
  37.  * @param hint1    輸入框的hint 文字提示信息 
  38.  * @param listener ok cancle 的點(diǎn)擊監(jiān)聽事件 
  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,實(shí)現(xiàn)不同dialog的生成

  1. /** 
  2.  * 根據(jù)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; 

新建監(jiān)聽接口MyDialogListener、MyItemDialogListener、MyCheckBoxItemDialogListener

編譯HAR包

利用Gradle可以將HarmonyOS Library庫模塊構(gòu)建為HAR包,構(gòu)建HAR包的方法如下:

在Gradle構(gòu)建任務(wù)中,雙擊PackageDebugHar或PackageReleaseHar任務(wù),構(gòu)建Debug類型或Release類型的HAR。

待構(gòu)建任務(wù)完成后,可以在工程目錄中的styledialog> bulid > outputs > har目錄中,獲取生成的HAR包。

【軟通動(dòng)力】HarmonyOS三方件開發(fā)指南(20)-Dialog組件-鴻蒙HarmonyOS技術(shù)社區(qū)

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-01-18 09:52:20

鴻蒙HarmonyOS開發(fā)

2021-02-04 09:45:19

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-01-12 12:04:40

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-01-21 13:21:18

鴻蒙HarmonyOSPhotoview組件

2021-01-20 09:54:56

鴻蒙HarmonyOS開發(fā)

2021-03-01 09:48:24

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-24 15:22:47

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-01-13 09:40:31

鴻蒙HarmonyOS開發(fā)

2021-02-04 13:06:38

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-04-16 09:28:18

鴻蒙HarmonyOS應(yīng)用

2021-01-22 17:33:03

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-05-12 15:17:39

鴻蒙HarmonyOS應(yīng)用

2021-02-26 14:15:27

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-03-01 14:01:41

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-03-19 17:42:01

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-04-20 09:42:20

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-03-31 09:50:25

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-04-12 09:36:54

鴻蒙HarmonyOS應(yīng)用

2021-03-10 15:03:40

鴻蒙HarmonyOS應(yīng)用

2021-08-02 14:54:50

鴻蒙HarmonyOS應(yīng)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)