HarmonyOS(鴻蒙)——單擊事件的四種寫法
一、簡介

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("哦,我被點擊了!"); } }
在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小時內刪除侵權內容。