(3.2)HarmonyOS鸿蒙双击事件
生活随笔
收集整理的這篇文章主要介紹了
(3.2)HarmonyOS鸿蒙双击事件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
跟單擊事件類似,雙擊事件也有4種寫法,這里采用當(dāng)前類作為實(shí)現(xiàn)類這種寫法,其他寫法可以參見《單擊事件的4種寫法》。不同的是雙擊事件需要的是Component.DoubleClickedListener。
實(shí)現(xiàn)步驟:
1.通過(guò)id找到組件。
2.給需要的組件設(shè)置雙擊事件。
3.本類實(shí)現(xiàn)DoubleClickedListener接口。
4.重寫onDoubleClicked方法。
①M(fèi)ainAbilitySlice.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);//找到按鈕。// 說(shuō)明:findComponentById返回的是父類對(duì)象所有組件,因此需要(Button)強(qiáng)轉(zhuǎn)。Button but = (Button) findComponentById(ResourceTable.Id_but);//找到文本框組件txt = (Text) findComponentById(ResourceTable.Id_txt);//給按鈕綁定一個(gè)雙擊事件but.setDoubleClickedListener(this);}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}@Overridepublic void onDoubleClick(Component component) {//component所有組件的父類//參數(shù)是被點(diǎn)擊的組件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="請(qǐng)雙擊我"ohos:text_size="120"/></DirectionalLayout>總結(jié)
以上是生活随笔為你收集整理的(3.2)HarmonyOS鸿蒙双击事件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 七猫小说如何添加书签
- 下一篇: (3.3)HarmonyOS鸿蒙长按事件