专栏名称: 51CTO技术栈
有趣 | 有料 | 有内涵,为您提供最优质的内容,愿我们一起悦享技术,成就人生。
目录
相关文章推荐
51好读  ›  专栏  ›  51CTO技术栈

我在鸿蒙上写了个抢红包Demo

51CTO技术栈  · 公众号  · 程序员  · 2020-11-06 18:07

正文

请到「今天看啥」查看全文


说下遇到的问题:


在 XML 中使用 PositionLayout 布局增加子组件后,子组件使用 setContentPosition(x,y) 造成定位失败。


无法父组件无法删除子组件,removeComponent() 无效。


无法隐藏组件 setVisibility();值是 4 或者 8 都无效。demo 需要删除元素,无奈只能设置元素的 width,height 为 0。


以下是代码片段:
"1.0" encoding="utf-8"?>
"http://schemas.huawei.com/res/ohos"
               ohos:width="match_parent"
               ohos:height="match_parent"
               ohos:id="$+id:p">
   "$+id:fenshu"
           ohos:text="分数:0"
           ohos:background_element="#66FF00"
           ohos:padding="10vp">
   



--------------------------------------------
package com.example.tv.slice;
import com.example.tv.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
//1920*1080
public class MainAbilitySlice extends AbilitySlice {
   int count = 0;
   @Override
   public void onStart(Intent intent)
{
       super.onStart(intent);
       super.setUIContent(ResourceTable.Layout_layout1);
       System.out.println("MainAbilitySlice>>>>>>>>>>>>>>>>>>>>>>onStart");
       Context context = this;
       PositionLayout positionLayout = (PositionLayout) findComponentById(ResourceTable.Id_p);
       Text text = (Text) findComponentById(ResourceTable.Id_fenshu);
       text.setContentPosition(1750, 0);
       text.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
       text.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
       Timer timer = new Timer();
       timer.scheduleAtFixedRate(new TimerTask() {
           public void run() {
               Random random = new Random();
               int rFenshu = random.nextInt(100);
               Button button = new Button(context);
               button.setWidth(100);
               button.setHeight(100);
               button.setText("红包:" + rFenshu);
               button.setClickedListener((c) -> {
                   button.setWidth(0);
                   button.setHeight(0);
                   count += rFenshu;
                   text.setText("分数:" + count);
               });
               ShapeElement shapeElement = new ShapeElement();
               shapeElement.setRgbColor(new RgbColor(255, 0, 0));
               button.setBackground(shapeElement);
               int i1 = random.nextInt(1720);
               button.setContentPosition(i1, 0);
               positionLayout.addComponent(button);
               AnimatorProperty animatorProperty = button.createAnimatorProperty();
               animatorProperty.moveToY(700).setDuration(4000);
               animatorProperty.setStateChangedListener(new Animator.StateChangedListener() {
                   @Override
                   public void onStart(Animator animator)
{
                   }
                   @Override
                   public void onStop(Animator animator)
{
                   }
                   @Override
                   public void onCancel(Animator animator)
{
                   }
                   @Override
                   public void onEnd(Animator animator)
{
                       button.setWidth(0);
                       button.setHeight(0);
                   }
                   @Override
                   public void onPause(Animator animator)
{
                   }
                   @Override
                   public void onResume(Animator animator)
{
                   }
               });
               animatorProperty.start();
           }
       }, 1000,1000);
   }
   @Override
   protected void onActive()
{
       super.onActive();
       System.out.println("MainAbilitySlice>>>>>>>>>>>>>>>>>>>>>>onActive");
   }
   @Override
   protected void onInactive()
{
       super.onInactive();
       System.out.println("MainAbilitySlice>>>>>>>>>>>>>>>>>>>>>>onInactive");
   }
   @Override
   protected void onBackground()
{
       super.onBackground();
       System.out.println("MainAbilitySlice>>>>>>>>>>>>>>>>>>>>>>onBackground");
   }
   @Override
   protected void onForeground(Intent intent)
{
       super.onForeground(intent);
       System.out.println("MainAbilitySlice>>>>>>>>>>>>>>>>>>>>>>onForeground");
   }
   @Override
   protected void onStop()
{
       super.onStop();
       System.out.println("MainAbilitySlice>>>>>>>>>>>>>>>>>>>>>>onStop");
   }
}

👇 扫码关注 HarmonyOS技术社区 👇

专注开源技术,共建鸿蒙生态



“阅读原文” 了解更多







请到「今天看啥」查看全文


推荐文章
经典短篇阅读小组  ·  情人节,来表个白吧~~~
8 年前
笑的合不拢嘴  ·  老公被抓,媳妇一句话让老公跪了
8 年前
摸摸艺术书  ·  柴晋宁 | 超级市场浮世绘
7 年前
美闻参阅  ·  情绪,就是心魔(深度好文)
7 年前