• 【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

  • 相关阅读:
    解决craco启动react项目卡死在Starting the development server的问题
    CentOS8.2 配置go开发环境
    Vue2.0+Vue3.0复习
    时间序列分析中的自相关
    源代码开发企业要如何进行代码加密,自主知识产权维护刻不容缓
    python Flask 缓存的两种方式
    数据结构:多栈共享技术
    ARFoundation系列讲解 - 70 HumanBodyTracking3D
    【心电信号】Simulink胎儿心电信号提取【含Matlab源码 1550期】
    【hadoop】mapreduce面试题总结
  • 原文地址:https://blog.csdn.net/weixin_44708240/article/details/126746155