> 文档中心 > 【JAVA UI】【HarmonyOS】 鸿蒙setBindStateChangedListener的基本使用

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


 参数讲解

setBindStateChangedListener(Component.BindStateChangedListener)

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

示例

findComponentById(ResourceTable.Id_text_helloworld).setBindStateChangedListener(new Component.BindStateChangedListener() {     @Override     public void onComponentBoundToWindow(Component component) {  //todo 当组件绑定到窗口时调用     }     @Override     public void onComponentUnboundFromWindow(Component component) {  //todo 当组件从窗口解除绑定时调用。     } });

问题描述】

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

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

【问题解答】

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

答:

参考如下链接

文档中心 | HarmonyOS

cke_3288.png

此api从Api Version 7 开始支持

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

答:

代码如下

textField.setBindStateChangedListener(new Component.BindStateChangedListener() {     @Override     public void onComponentBoundToWindow(Component component) {  int count1=textField.getLineCount();  Text mytext=findComponentById(ResourceTable.Id_mytext);  mytext.setText("#####行数"+count1);     }     @Override     public void onComponentUnboundFromWindow(Component component) {     } });

【问题描述】

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

【问题解答】

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

findComponentById(ResourceTable.Id_text_helloworld).setBindStateChangedListener(new Component.BindStateChangedListener() {     @Override     public void onComponentBoundToWindow(Component component) {  AnimationImage = findComponentById(ResourceTable.Id_text_helloworld);  AnimationImage.setRotation(0);  AnimatorProperty animator = AnimationImage.createAnimatorProperty();  animator.setCurveType(Animator.CurveType.LINEAR);  animator.setLoopedCount(AnimatorValue.INFINITE);  animator.rotate(360);  animator.setDuration(10000);  animator.start();     }     @Override     public void onComponentUnboundFromWindow(Component component) {     } });

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

杭州女装网