RadioContainer是RadioButton的容器,在其包裹下的RadioButton保證只有一個被選項(xiàng)。本質(zhì)就是一個DirectionalLayout
RadioButton用于多選一的操作,需要搭配RadioContainer使用,實(shí)現(xiàn)單選效果。本質(zhì)就是一個左邊帶圖片的Button。
RadioButton,RadioContainer也可以看做是一個DirectionalLayout有一個或多個的Button
RadioButton
常用屬性
marked:當(dāng)前狀態(tài),boolean類型可以,對應(yīng)方法setChecked設(shè)置當(dāng)前狀態(tài),isChecked獲取當(dāng)前狀態(tài)
text_color_on:處于選中狀態(tài)的文本顏色,對應(yīng)方法setTextColorOn
text_color_off:處于未選中狀態(tài)的文本顏色,對應(yīng)方法setTextColorOff
check_element:狀態(tài)標(biāo)志樣式Element類型,可設(shè)置RadioButton選中和未選中的左邊圖片樣式,
RadioContainer
常用方法
RadioContainer container = (RadioContainer) findComponentById(ResourceTable.Id_radio_container);
//設(shè)置選中的RadioButton
container.mark(0);
//清除RadioContainer中所有RadioButton的選定狀態(tài)。
container.cancelMarks();
//設(shè)置響應(yīng)RadioContainer狀態(tài)改變的事件。
container.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
? ? @Override
? ? public void onCheckedChanged(RadioContainer radioContainer, int index) {
//調(diào)用cancelMarks時(shí),index為-1
? ? }
});
//設(shè)置布局方向?yàn)闄M向布局效果
container.setOrientation(Component.HORIZONTAL);
效果圖:
默認(rèn)選中第二個RadioButton

點(diǎn)擊第一個RadioButton

清除選中RadioButton

相關(guān)代碼:
布局文件
<?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:bt"
? ? ? ? ohos:height="match_content"
? ? ? ? ohos:width="match_content"
? ? ? ? ohos:text="清除"
? ? ? ? ohos:text_size="50"/>
<RadioContainer
? ? ? ? ohos:id="$+id:rc"
? ? ? ? ohos:height="match_content"
? ? ? ? ohos:width="match_content"
? ? ? ? ohos:margin="50"
? ? ? ? >
<RadioButton
? ? ? ? ? ? ohos:id="$+id:rb1"
? ? ? ? ? ? ohos:height="match_content"
? ? ? ? ? ? ohos:width="match_content"
? ? ? ? ? ? ohos:check_element="$graphic:select"
? ? ? ? ? ? ohos:text="A"
? ? ? ? ? ? ohos:text_size="50">
</RadioButton>
<RadioButton
? ? ? ? ? ? ohos:id="$+id:rb2"
? ? ? ? ? ? ohos:height="match_content"
? ? ? ? ? ? ohos:width="match_content"
? ? ? ? ? ? ohos:check_element="$graphic:select"
? ? ? ? ? ? ohos:text="B"
? ? ? ? ? ? ohos:text_size="50">
</RadioButton>
<RadioButton
? ? ? ? ? ? ohos:id="$+id:rb3"
? ? ? ? ? ? ohos:height="match_content"
? ? ? ? ? ? ohos:width="match_content"
? ? ? ? ? ? ohos:check_element="$graphic:select"
? ? ? ? ? ? ohos:text="C"
? ? ? ? ? ? ohos:text_size="50">
</RadioButton>
</RadioContainer>
</DirectionalLayout>
select.xml代碼
<?xml version="1.0" encoding="UTF-8" ?>
<state-container xmlns:ohos="http://schemas.huawei.com/res/ohos">
選中樣式
<item ohos:element="#123456" ohos:state="component_state_checked"/>
未選中/狀態(tài)為空的樣式
<item ohos:element="#FFFF0000" ohos:state="component_state_empty"? />
</state-container>
java代碼
public class MainAbilitySliceextends AbilitySlice {
@Override
? ? public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
RadioContainer rc = (RadioContainer) findComponentById(ResourceTable.Id_rc);
rc.mark(1);
rc.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
@Override
? ? ? ? ? ? public void onCheckedChanged(RadioContainer radioContainer,int index) {
showToast("index=" + index +"? ? " +rc.getMarkedButtonId());
}
});
findComponentById(ResourceTable.Id_bt).setClickedListener(new Component.ClickedListener() {
@Override
? ? ? ? ? ? public void onClick(Component component) {
rc.cancelMarks();
}
});
}
public void showToast(String msg) {
ToastDialog dialog =new ToastDialog(getContext());
dialog.setAlignment(LayoutAlignment.CENTER);
dialog.setText(msg);
dialog.show();
}
}