(3.3)HarmonyOS鸿蒙长按事件
生活随笔
收集整理的這篇文章主要介紹了
(3.3)HarmonyOS鸿蒙长按事件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
跟單擊事件類似,長按事件也有4種寫法,這里采用當前類作為實現類這種寫法,其他寫法可以參見《單擊事件的4種寫法》。
實現步驟:
1.通過id找到組件。
2.給需要的組件設置長按事件。
3.本類實現LongClickedListener接口。
4.重寫onLongClicked方法。
①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.LongClickedListener{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.setLongClickedListener(this);}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}@Overridepublic void onLongClicked(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.3)HarmonyOS鸿蒙长按事件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (3.2)HarmonyOS鸿蒙双击事件
- 下一篇: (3.4)HarmonyOS鸿蒙滑动事件