某音点赞业务逻辑在Harmony实现-Java 原创

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

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

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout

    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:id="$+id:ld"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="vertical">

    <Image
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:id="$+id:img"
        ohos:image_src="$media:xin_off"
        ohos:background_element="#d9d9d9"
    />

</DirectionalLayout>

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

业务实现:
业务实现
这里需要给两个控件注册监听事件,分别是DirectionalLayout控件,这里代表页面的最外层容器,此处可以理解为我们双击的“屏幕”;还有一个是小爱心,也就是图片控件。
二者需要添加的监听事件不一样,DirectionalLayout控件需要注册的是双击事件;图片控件需要注册的是单击事件。

package com.example.douyin_click.slice;

import com.example.douyin_click.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.ClickedListener, Component.DoubleClickedListener {
    DirectionalLayout directionalLayout;
    Image image;
    boolean flag = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        directionalLayout = (DirectionalLayout)this.findComponentById(ResourceTable.Id_ld);
        image = (Image) this.findComponentById(ResourceTable.Id_img);
        directionalLayout.setDoubleClickedListener(this);
        image.setClickedListener(this);
    }



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

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
    /**
     * 单击事件方法
     *
     * @param component
     */
    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_img) {
            flag = !flag;
            image.setImageAndDecodeBounds(flag ? ResourceTable.Media_xin : ResourceTable.Media_xin_off);
        }
    }

    /**
     * 双击事件方法
     *
     * @param component
     */
    @Override
    public void onDoubleClick(Component component) {
        // 双击修改Image图片,如果未点赞,则修改为红色小红心,如果已点赞则不处理
        if (component.getId() == ResourceTable.Id_ld && !flag) {
            image.setImageAndDecodeBounds(ResourceTable.Media_xin);
        }
    }
}

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.

某音点赞业务逻辑在Harmony实现-Java-鸿蒙开发者社区

某音点赞业务逻辑在Harmony实现-Java-鸿蒙开发者社区

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