• 【JAVA UI】【HarmonyOS】 鸿蒙setBindStateChangedListener的基本使用


     参数讲解

    setBindStateChangedListener(Component.BindStateChangedListener)

    方法说明:该组件是否添加到窗口的组件树上

    示例

    1. findComponentById(ResourceTable.Id_text_helloworld).setBindStateChangedListener(new Component.BindStateChangedListener() {
    2. @Override
    3. public void onComponentBoundToWindow(Component component) {
    4. //todo 当组件绑定到窗口时调用
    5. }
    6. @Override
    7. public void onComponentUnboundFromWindow(Component component) {
    8. //todo 当组件从窗口解除绑定时调用。
    9. }
    10. });

    【问题描述】

    问题1:调用TextField.getLineCount()获取行数闪退

    问题2:怎么使用getLineCount(),

    【问题解答】

    问题1:调用TextField.getLineCount()获取行数闪退

    答:

    参考如下链接

    文档中心 | HarmonyOS

    cke_3288.png

    此api从Api Version 7 开始支持

    问题2:怎么使用getLineCount(),

    答:

    代码如下

    1. textField.setBindStateChangedListener(new Component.BindStateChangedListener() {
    2. @Override
    3. public void onComponentBoundToWindow(Component component) {
    4. int count1=textField.getLineCount();
    5. Text mytext=findComponentById(ResourceTable.Id_mytext);
    6. mytext.setText("#####行数"+count1);
    7. }
    8. @Override
    9. public void onComponentUnboundFromWindow(Component component) {
    10. }
    11. });

    【问题描述】

    首页有个动画-安装应用后自动启动动画,执行启动的代码,然后该动画不能启动,单给在点击事件中执行动画的代码,动画生效,这是什么原因?应该怎么处理呢?

    【问题解答】

    当组件没有添加到窗口的组件树,支持该动画是不生效,需要监听该组件是否添加到窗口的组件树上,代码如下

    1. findComponentById(ResourceTable.Id_text_helloworld).setBindStateChangedListener(new Component.BindStateChangedListener() {
    2. @Override
    3. public void onComponentBoundToWindow(Component component) {
    4. AnimationImage = findComponentById(ResourceTable.Id_text_helloworld);
    5. AnimationImage.setRotation(0);
    6. AnimatorProperty animator = AnimationImage.createAnimatorProperty();
    7. animator.setCurveType(Animator.CurveType.LINEAR);
    8. animator.setLoopedCount(AnimatorValue.INFINITE);
    9. animator.rotate(360);
    10. animator.setDuration(10000);
    11. animator.start();
    12. }
    13. @Override
    14. public void onComponentUnboundFromWindow(Component component) {
    15. }
    16. });

     欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

  • 相关阅读:
    Git版本管理
    使用SWIG编写python的扩展
    LeetCode 第 311 场周赛
    css继承属性
    学习编写代码的挑战与经验
    谷粒商城实战(008 缓存)
    appuploader登录方法详解
    浅谈Java中的代理模式
    k8s named Kubernetes
    Flutter自带国际化适配自动生成方案
  • 原文地址:https://blog.csdn.net/weixin_44708240/article/details/126746155