中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
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; } } }
双击实现点赞
双击取消点赞
微信扫码分享