鸿蒙应用-Java实现双击屏幕点赞和取消 原创

HUAWEI_Engineer
发布于 2022-11-6 12:06
浏览
0收藏

1.页面布局
这一步中需要使用到一个标签,标签是图片控件,我们可以通过设置标签中图片的改变来标志点赞与取消点赞的状态,此外由于我们需要双击屏幕,这个时候我们需要使用到控件最外层的DirectionalLayout控件,因此我们给DirectionalLayout控件添加ID,便于定位。

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    ohos:id="$+id:ld"
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="vertical">
//设置图片
    <Image
        ohos:id="$+id:img"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:max_height="600px"
        ohos:max_width="300px"
        ohos:background_element="cyan"
        ohos:image_src="$media:xin_off"
        />


</DirectionalLayout>

package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;

public class MainAbilitySlice extends AbilitySlice implements Component.DoubleClickedListener {
    Image image;
    @Override

    public void onStart(Intent intent) {

        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        //使用findComponentById的方法找到img的id
        image = (Image)findComponentById(ResourceTable.Id_img);
        DirectionalLayout directionalLayout = (DirectionalLayout)findComponentById(ResourceTable.Id_ld) ;
        directionalLayout.setDoubleClickedListener(this);
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
    //设置flag标记
    boolean flag  = false;

    @Override
    public void onDoubleClick(Component component) {
        //使用的image方法
        if(flag) {
            image.setImageAndDecodeBounds(ResourceTable.Media_xin_off);
            flag = false;
        }else{
            image.setImageAndDecodeBounds(ResourceTable.Media_xin);
            flag = true;
        }
 }
}

双击实现点赞
鸿蒙应用-Java实现双击屏幕点赞和取消-鸿蒙开发者社区

双击取消点赞
鸿蒙应用-Java实现双击屏幕点赞和取消-鸿蒙开发者社区

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
双击点赞.rar 1.22M 0次下载
1
收藏
回复
举报
回复
    相关推荐