HarmonyOS鴻蒙)——單擊事件的四種寫法

      網友投稿 1563 2025-04-01

      一、簡介


      HarmonyOS(鴻蒙)開發過程中,使用到的最多的事件就是單擊事件,單擊事件一共有四種寫法,它們有一些細微的區別和場景。

      四種寫法如下:

      定義實現類

      當前類作為實現類

      匿名內部類

      方法引用

      二、定義實現類

      定義實現類ClickedListener實現Component.ClickedListener接口并且重寫onClick方法

      /** * 實現ClickedListener接口并重寫onClick方法 */ class ClickedListener implements Component.ClickedListener { /** * 點擊事件觸發的操作會調用的方法 * @param component 被點擊的組件對象 */ @Override public void onClick(Component component) { // 具體點擊操作的邏輯處理 Button button = (Button) component; button.setText("哦,我被點擊了!"); } }

      HarmonyOS(鴻蒙)——單擊事件的四種寫法

      在MainAbilitySlice中通過控件的setClickedListener方法傳入Component.ClickedListener接口的實現類ClickedListener

      public class MainAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); //1. 找到組件 Button button = (Button) this.findComponentById(ResourceTable.Id_button); //2. 綁定單擊事件 button.setClickedListener(new ClickedListener()); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }

      三、當前類作為實現類

      直接使用當前類MainAbilitySlice作為Component.ClickedListener接口的實現類,這個與上面的區別在于,我們不需要單獨定義一個實現類,同時可以在onClick方法中共享MainAbilitySlice的定義的相關變量

      public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); //1. 找到組件 Button button = (Button) this.findComponentById(ResourceTable.Id_button); //2. 綁定單擊事件 button.setClickedListener(this); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } @Override public void onClick(Component component) { // 具體點擊操作的邏輯處理 Button button = (Button) component; button.setText("哦,我被點擊了!"); } }

      四、匿名內部類

      直接setClickedListener方法中傳入匿名內部類,new Component.ClickedListener() {},這樣做的壞處在于代碼不可重復使用,并且如果這樣的寫法過多,會導致代碼可讀性降低

      public class MainAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); //1. 找到組件 Button button = (Button) this.findComponentById(ResourceTable.Id_button); //2. 綁定單擊事件 button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { // 具體點擊操作的邏輯處理 Button button = (Button) component; button.setText("哦,我被點擊了!"); } }); /* * lambda寫法 button.setClickedListener(component -> { // 具體點擊操作的邏輯處理 Button button1 = (Button) component; button1.setText("哦,我被點擊了!"); }); */ } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }

      五、方法引用

      這種寫法,無需新增類,MainAbilitySlice類也無需實現Component.ClickedListener接口,而是通過手動寫一個同名、同參數的onClick方法,通過方法引用的方式來實現 button.setClickedListener(this::onClick);

      public class MainAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); //1. 找到組件 Button button = (Button) this.findComponentById(ResourceTable.Id_button); //2. 綁定單擊事件 button.setClickedListener(this::onClick); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } public void onClick(Component component) { // 具體點擊操作的邏輯處理 Button button = (Button) component; button.setText("哦,我被點擊了!"); } }

      Java 分布式 移動APP

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:電子表格為什么打不開(電子表格為什么打不開了)
      下一篇:如何理解excel函數嵌套
      相關文章
      久久亚洲综合色一区二区三区| 亚洲中文字幕无码久久2017| 国产亚洲精品观看91在线| 日韩国产精品亚洲а∨天堂免| 亚洲av成人综合网| 亚洲白色白色永久观看| 91亚洲精品第一综合不卡播放| 亚洲AV日韩精品久久久久| 国产av无码专区亚洲av桃花庵| 亚洲午夜福利AV一区二区无码| 亚洲热妇无码AV在线播放| 亚洲国产精品乱码一区二区| 亚洲国产精品无码专区| 亚洲大成色www永久网站| 人人狠狠综合久久亚洲婷婷| 亚洲色成人WWW永久网站| 亚洲精品无码久久久久sm| 人人狠狠综合久久亚洲88| 亚洲AV无码日韩AV无码导航| 久久精品国产96精品亚洲| 亚洲视频一区在线观看| 亚洲综合色一区二区三区小说| 亚洲国产成人久久精品app| 亚洲人成在线播放| 亚洲乱人伦中文字幕无码| 爱情岛亚洲论坛在线观看 | 久久噜噜噜久久亚洲va久| 亚洲一区中文字幕久久| 亚洲综合色丁香麻豆| 亚洲人成www在线播放| 亚洲GV天堂无码男同在线观看| 国产亚洲精品2021自在线| 亚洲人成无码网站久久99热国产| 国产亚洲精久久久久久无码77777| 亚洲中文字幕日产乱码高清app| 亚洲成AV人片在线观看无码| 亚洲AV日韩AV天堂久久| 亚洲电影唐人社一区二区| 国产精品亚洲精品观看不卡| 亚洲av无码片vr一区二区三区| 亚洲国产香蕉人人爽成AV片久久|