日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

文章目錄

  • 一、布局文件中配置 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。