【鸿蒙 HarmonyOS】UI 组件 ( 多选按钮 | Checkbox 组件 )
文章目錄
- 一、布局文件中配置 Checkbox 組件
- 二、代碼中配置 Checkbox 組件選中事件
- 三、完整代碼示例
- 四、GitHub 地址
一、布局文件中配置 Checkbox 組件
Checkbox 組件就是多選按鈕 ;
Checkbox 多選按鈕之間不存在互斥關(guān)系 , 可以 同時選擇 ;
如 : 給出 333 個 Checkbox 按鈕 , 可以同時選中其中的 000 個 , 111 個 , 222 個 , 333 個 ;
布局文件配置 Checkbox :
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><Checkboxohos:id="$+id:checkbox0"ohos:height="match_content"ohos:width="match_content"ohos:text="多選按鈕 0"ohos:text_size="100"/> </DirectionalLayout>二、代碼中配置 Checkbox 組件選中事件
調(diào)用 Checkbox 對象的 setCheckedStateChangedListener 方法設置 選中 / 取消選中 的 AbsButton.CheckedStateChangedListener 監(jiān)聽器 , 當用戶 選中 / 取消選中 時 , 會回調(diào)上述監(jiān)聽器的 onCheckedChanged 方法 , 其中第二個參數(shù) boolean b , b 為 true 多選按鈕選中 , false 取消選中 ;
代碼示例 :
// 獲取 XML 布局中的 Checkbox 多選按鈕Checkbox checkbox0 = (Checkbox) findComponentById(ResourceTable.Id_checkbox0);checkbox0.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {@Overridepublic void onCheckedChanged(AbsButton absButton, boolean b) {// b 為 true 多選按鈕選中 , false 取消選中if(b) {text.setText("當前 多選按鈕 0 選中狀態(tài) : 選中");}else{text.setText("當前 多選按鈕 0 選中狀態(tài) : 未選中");}}});三、完整代碼示例
布局文件代碼示例 :
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><Checkboxohos:id="$+id:checkbox0"ohos:height="match_content"ohos:width="match_content"ohos:text="多選按鈕 0"ohos:text_size="100"/><Checkboxohos:id="$+id:checkbox1"ohos:height="match_content"ohos:width="match_content"ohos:text="多選按鈕 1"ohos:text_size="100"/><Checkboxohos:id="$+id:checkbox2"ohos:height="match_content"ohos:width="match_content"ohos:text="多選按鈕 2"ohos:text_size="100"/><Textohos:id="$+id:text"ohos:height="match_content"ohos:width="match_content"ohos:layout_alignment="horizontal_center"ohos:text="當前 多選按鈕 0 選中狀態(tài) : 未選中"ohos:text_size="50"ohos:text_color="#FF0000"/></DirectionalLayout>Java 代碼示例 :
package com.example.checkbox.slice;import com.example.checkbox.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.AbsButton; import ohos.agp.components.Button; import ohos.agp.components.Checkbox; import ohos.agp.components.Text;public class MainAbilitySlice extends AbilitySlice {@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);// 獲取文本組件Text text = (Text) findComponentById(ResourceTable.Id_text);// 獲取 XML 布局中的 Checkbox 多選按鈕Checkbox checkbox0 = (Checkbox) findComponentById(ResourceTable.Id_checkbox0);checkbox0.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {@Overridepublic void onCheckedChanged(AbsButton absButton, boolean b) {// b 為 true 多選按鈕選中 , false 取消選中if(b) {text.setText("當前 多選按鈕 0 選中狀態(tài) : 選中");}else{text.setText("當前 多選按鈕 0 選中狀態(tài) : 未選中");}}});}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);} }運行結(jié)果 :
四、GitHub 地址
GitHub 主應用 : https://github.com/han1202012/HarmonyHelloWorld
CheckBox 組件示例 Module : https://github.com/han1202012/HarmonyHelloWorld/tree/master/checkbox
總結(jié)
以上是生活随笔為你收集整理的【鸿蒙 HarmonyOS】UI 组件 ( 多选按钮 | Checkbox 组件 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【鸿蒙 HarmonyOS】UI 组件
- 下一篇: 【鸿蒙 HarmonyOS】UI 组件