(3.2)HarmonyOS鸿蒙双击事件
生活随笔
收集整理的這篇文章主要介紹了
(3.2)HarmonyOS鸿蒙双击事件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
跟單擊事件類似,雙擊事件也有4種寫法,這里采用當前類作為實現類這種寫法,其他寫法可以參見《單擊事件的4種寫法》。不同的是雙擊事件需要的是Component.DoubleClickedListener。
實現步驟:
1.通過id找到組件。
2.給需要的組件設置雙擊事件。
3.本類實現DoubleClickedListener接口。
4.重寫onDoubleClicked方法。
①MainAbilitySlice.java文件
package com.example.yeman.slice;import com.example.yeman.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Button; import ohos.agp.components.Component; import ohos.agp.components.Text;public class MainAbilitySlice extends AbilitySlice implements Component.DoubleClickedListener{Text txt;@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);//找到按鈕。// 說明:findComponentById返回的是父類對象所有組件,因此需要(Button)強轉。Button but = (Button) findComponentById(ResourceTable.Id_but);//找到文本框組件txt = (Text) findComponentById(ResourceTable.Id_txt);//給按鈕綁定一個雙擊事件but.setDoubleClickedListener(this);}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}@Overridepublic void onDoubleClick(Component component) {//component所有組件的父類//參數是被點擊的組件txt.setText("你雙擊了按鈕");} }②ability_main.xml文件
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="center"ohos:orientation="vertical"><Textohos:id="$+id:txt"ohos:height="match_content"ohos:width="match_content"ohos:text="這是文本框組件"ohos:text_size="100"/><Buttonohos:id="$+id:but"ohos:height="match_content"ohos:width="match_content"ohos:background_element="blue"ohos:text="請雙擊我"ohos:text_size="120"/></DirectionalLayout>總結
以上是生活随笔為你收集整理的(3.2)HarmonyOS鸿蒙双击事件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 七猫小说如何添加书签
- 下一篇: (3.3)HarmonyOS鸿蒙长按事件