回复
判断组件当前状态
footballboy
发布于 2021-4-13 10:45
浏览
0收藏
setComponentStateChangedListener方法判断组件当前状态
今天看到有坛友问到按钮点击调用setComponentStateChangedListener方法监控组件状态改变,触发了两次这个是正常吗?那接下来我来分享一下我的理解,有不对的请指出,谢谢!
首先我们先看布局页面,就是个简单的按钮,代码如下
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Button
ohos:id="$+id:btnStateTest"
ohos:height="match_content"
ohos:width="match_content"
ohos:text_size="30vp"
ohos:background_element="gray"
ohos:center_in_parent="true"
ohos:text="测试按钮"
ohos:bottom_margin="10vp"></Button>
</DirectionalLayout>
然后我们看Java代码如下
package com.example.phonetest.slice;
import com.example.phonetest.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentState;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
public class ButtonComponentStateTestSlice extends AbilitySlice {
static final HiLogLabel label=new HiLogLabel(HiLog.LOG_APP,0x0001,"按钮状态测试");
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_button_component_state_test);
Button btnStateTest=(Button)findComponentById(ResourceTable.Id_btnStateTest);
HiLog.info(label,"按钮Id:"+btnStateTest.getId());
btnStateTest.setComponentStateChangedListener((c,i)->{
HiLog.info(label,"组件Id:"+c.getId());
HiLog.info(label,"当前ViewState:"+i);
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
然后我们看一下点击按钮打印的日志,如下图
为了验证是否都是同一个按钮测试的,我也将按钮的Id打出来了,我们可以看出事件方法里面触发的两次是同一个按钮,那么这两次状态改变是针对哪些状态呢,我找到ComponentState类的状态如下图,
看了以后一脸蒙啊,完全对不上,这怎么搞,后来研究了半天,发现这个类里面还有个方法名叫isStateMatched,然后我想了一下是不是要用这个去匹配,然后我在setComponentStateChangedListener方法里面加入了如下测试代码
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_CHECKED,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_PRESSED");
return;
}
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_DISABLED,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_DISABLED");
return;
}
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_EMPTY,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_EMPTY");
return;
}
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_FOCUSED,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_FOCUSED");
return;
}
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_HOVERED,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_HOVERED");
return;
}
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_PRESSED,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_PRESSED");
return;
}
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_SELECTED,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_PRESSED");
return;
}
HiLog.info(label,"未匹配任何状态");
然后我再次单击测试按钮然后,我们看一下点击效果
然后得出的结论按钮为什么触发了两次,一次是按钮按下的状态,按下以后然后是恢复原来的未知状态。
接下来我又添加了几个按钮测试了一下手动禁用和手动按下状态,完全代码如下
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Button
ohos:id="$+id:btnStateTest"
ohos:height="match_content"
ohos:width="match_content"
ohos:text_size="30vp"
ohos:background_element="gray"
ohos:center_in_parent="true"
ohos:text="测试按钮"
ohos:bottom_margin="10vp"></Button>
<Button
ohos:id="$+id:btnStateDisabled"
ohos:height="match_content"
ohos:width="match_content"
ohos:text_size="30vp"
ohos:background_element="gray"
ohos:center_in_parent="true"
ohos:text="禁用测试按钮"
ohos:bottom_margin="10vp"></Button>
<Button
ohos:id="$+id:btnStateEnable"
ohos:height="match_content"
ohos:width="match_content"
ohos:text_size="30vp"
ohos:background_element="gray"
ohos:center_in_parent="true"
ohos:text="启用测试按钮"
ohos:bottom_margin="10vp"></Button>
<Button
ohos:id="$+id:btnStatePressed"
ohos:height="match_content"
ohos:width="match_content"
ohos:text_size="30vp"
ohos:background_element="gray"
ohos:center_in_parent="true"
ohos:text="手动设置测试按钮按下"
ohos:bottom_margin="10vp"></Button>
<Button
ohos:id="$+id:btnStateCancelPressed"
ohos:height="match_content"
ohos:width="match_content"
ohos:text_size="30vp"
ohos:background_element="gray"
ohos:center_in_parent="true"
ohos:text="手动取消测试按钮按下"
ohos:bottom_margin="10vp"></Button>
</DirectionalLayout>
Java代码如下
package com.example.phonetest.slice;
import com.example.phonetest.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentState;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
public class ButtonComponentStateTestSlice extends AbilitySlice {
static final HiLogLabel label=new HiLogLabel(HiLog.LOG_APP,0x0001,"按钮状态测试");
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_button_component_state_test);
Button btnStateTest=(Button)findComponentById(ResourceTable.Id_btnStateTest);
HiLog.info(label,"按钮Id:"+btnStateTest.getId());
btnStateTest.setComponentStateChangedListener((c,i)->{
HiLog.info(label,"组件Id:"+c.getId());
HiLog.info(label,"当前ViewState:"+i);
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_CHECKED,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_PRESSED");
return;
}
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_DISABLED,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_DISABLED");
return;
}
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_EMPTY,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_EMPTY");
return;
}
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_FOCUSED,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_FOCUSED");
return;
}
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_HOVERED,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_HOVERED");
return;
}
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_PRESSED,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_PRESSED");
return;
}
if(ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_SELECTED,i)) {
HiLog.info(label,"匹配ComponentState.COMPONENT_STATE_PRESSED");
return;
}
HiLog.info(label,"未匹配任何状态");
});
btnStateTest.setPressState(true);
btnStateTest.setPressState(false);
Button btnStateDisabled=(Button)findComponentById(ResourceTable.Id_btnStateDisabled);
btnStateDisabled.setClickedListener(c->{
HiLog.info(label,"禁用测试按钮");
btnStateTest.setEnabled(false);
});
Button btnStateEnable=(Button)findComponentById(ResourceTable.Id_btnStateEnable);
btnStateEnable.setClickedListener(c->{
HiLog.info(label,"启用测试按钮");
btnStateTest.setEnabled(true);
});
Button btnStatePressed=(Button)findComponentById(ResourceTable.Id_btnStatePressed);
btnStatePressed.setClickedListener(c->{
HiLog.info(label,"设置为pressState");
btnStateTest.setPressState(true);
});
Button btnStateCancelPressed=(Button)findComponentById(ResourceTable.Id_btnStateCancelPressed);
btnStateCancelPressed.setClickedListener(c->{
HiLog.info(label,"取消pressState");
btnStateTest.setPressState(false);
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
然后在手动触发按下按钮,你又会发现一个有趣的现象就是直接在onStart方法里面调用setPressState方法和在按钮里面调用setPressState方法监控到状态码竟是不一样的,代码如下图
运行结果如下图
所以肯定要靠isStateMatched来判断。
已于2021-4-13 10:45:26修改
赞
收藏
回复
相关推荐