日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【鸿蒙 HarmonyOS】UI 组件 ( 多选按钮 | Checkbox 组件 )

發布時間:2025/6/17 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【鸿蒙 HarmonyOS】UI 组件 ( 多选按钮 | Checkbox 组件 ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 一、布局文件中配置 Checkbox 組件
  • 二、代碼中配置 Checkbox 組件選中事件
  • 三、完整代碼示例
  • 四、GitHub 地址





一、布局文件中配置 Checkbox 組件



Checkbox 組件就是多選按鈕 ;

Checkbox 多選按鈕之間不存在互斥關系 , 可以 同時選擇 ;

如 : 給出 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 組件選中事件



調用 Checkbox 對象的 setCheckedStateChangedListener 方法設置 選中 / 取消選中 的 AbsButton.CheckedStateChangedListener 監聽器 , 當用戶 選中 / 取消選中 時 , 會回調上述監聽器的 onCheckedChanged 方法 , 其中第二個參數 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 選中狀態 : 選中");}else{text.setText("當前 多選按鈕 0 選中狀態 : 未選中");}}});



三、完整代碼示例



布局文件代碼示例 :

<?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 選中狀態 : 未選中"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 選中狀態 : 選中");}else{text.setText("當前 多選按鈕 0 選中狀態 : 未選中");}}});}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);} }

運行結果 :






四、GitHub 地址



GitHub 主應用 : https://github.com/han1202012/HarmonyHelloWorld

CheckBox 組件示例 Module : https://github.com/han1202012/HarmonyHelloWorld/tree/master/checkbox

總結

以上是生活随笔為你收集整理的【鸿蒙 HarmonyOS】UI 组件 ( 多选按钮 | Checkbox 组件 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。