media
下
id
ability_main
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
ohos:id="$+id:dl"
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:image_src="$media:white"
ohos:background_element="cyan"
>
</Image>
</DirectionalLayout>
MainAbilitySlice
package com.xdr630.listenerapplication6.slice;
import com.xdr630.listenerapplication6.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);
//1.找到图片组件
image = (Image) findComponentById(ResourceTable.Id_img);
//找到铺满屏幕布局的对象
DirectionalLayout dl = (DirectionalLayout) findComponentById(ResourceTable.Id_dl);
//2.给布局添加双击事件
dl.setDoubleClickedListener(this);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
//如果标记为false,表示没有点赞,此时把白色变为红色
//如果标记为true,表示已经点赞,再次双击后,会把红色变回白色
boolean flag = false;
@Override
public void onDoubleClick(Component component) {
//修改图片的红星就可以了,只需要用到image就行了,所以把image定为成员变量
if (flag){
image.setImageAndDecodeBounds(ResourceTable.Media_white);
//取消点赞变成白色,也要把flag设置为false
flag = false;
}else{
image.setImageAndDecodeBounds(ResourceTable.Media_red);
//当启动项目的时候,flag初始值是false,就会走下面的else的代码,变成红色后就要把flag变成true了
flag = true;
}
}
}
业务分析:
实现思路:
else
代码里设置为红色后就把 flag
取反去掉,就不会出现再次双击取消点赞了。flag
变为相反的操作package com.xdr630.listenerapplication6.slice;
import com.xdr630.listenerapplication6.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, Component.ClickedListener {
Image image;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//1.找到图片组件
image = (Image) findComponentById(ResourceTable.Id_img);
//找到铺满屏幕布局的对象
DirectionalLayout dl = (DirectionalLayout) findComponentById(ResourceTable.Id_dl);
//2.给布局添加双击事件
dl.setDoubleClickedListener(this);
//3.给图片添加单击事件
image.setClickedListener(this);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
//如果标记为false,表示没有点赞,此时把白色变为红色
//如果标记为true,表示已经点赞,再次双击后,会把红色变回白色
boolean flag = false;
@Override
public void onDoubleClick(Component component) {
//修改图片的红星就可以了,只需要用到image就行了,所以把image定为成员变量
if (flag){
image.setImageAndDecodeBounds(ResourceTable.Media_white);
//取消点赞变成白色,也要把flag设置为false
flag = false;
}else{
image.setImageAndDecodeBounds(ResourceTable.Media_red);
//当启动项目的时候,flag初始值是false,就会走下面的else的代码,此时设置为红色,把flag去掉,再次双击后就还是红色了
}
}
@Override
public void onClick(Component component) {
if (flag){
image.setImageAndDecodeBounds(ResourceTable.Media_white);
flag = false;
}else{
image.setImageAndDecodeBounds(ResourceTable.Media_red);
flag = true;
}
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。