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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【鸿蒙 HarmonyOS】UI 组件 ( 单选按钮 | RadioButton 与 RadioContainer 组件 )

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

文章目錄

  • 一、RadioButton 與 RadioContainer 組件
  • 二、監聽 RadioContainer 選擇事件
  • 三、GitHub 地址





一、RadioButton 與 RadioContainer 組件



RadioButton 組件就是單選按鈕 ;

給出 333 個 RadioButton 按鈕 , 使用 RadioContainer 編組后 , 只能 333111 , 同一時刻 , 只能有單個按鈕處于選中狀態 ;


RadioContainer 組件是單選按鈕的編組組件 , 可以將若干 RadioButton 放到 RadioContainer 標簽中 , 這些 RadioButton 組件只能有一個處于選中狀態 ;


RadioContainer 編組 RadioButton 布局代碼示例 :

<?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"><!-- 單選按鈕容器 --><RadioContainerohos:id="$+id:radioContainer"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><RadioButtonohos:id="$+id:radioButton1"ohos:height="match_content"ohos:width="match_content"ohos:layout_alignment="horizontal_center"ohos:text="單選按鈕 1"ohos:text_size="100"/><RadioButtonohos:id="$+id:radioButton2"ohos:height="match_content"ohos:width="match_content"ohos:layout_alignment="horizontal_center"ohos:text="單選按鈕 2"ohos:text_size="100"/><RadioButtonohos:id="$+id:radioButton3"ohos:height="match_content"ohos:width="match_content"ohos:layout_alignment="horizontal_center"ohos:text="單選按鈕 3"ohos:text_size="100"/></RadioContainer></DirectionalLayout>

顯示樣式 : 當前單選按鈕 2 處于選中狀態 ;

下圖是使用遠程鴻蒙模擬器顯示單選按鈕 ;





二、監聽 RadioContainer 選擇事件



調用 RadioContainer 對象的 setMarkChangedListener 方法 , 給 RadioContainer 添加 RadioContainer.CheckedStateChangedListener 監聽器 , 可以監聽編組在 RadioContainer 下的所有 RadioButton 的選擇事件 ;

當某個 RadioButton 被點擊時 , 會回調 RadioContainer.CheckedStateChangedListener 監聽器的 onCheckedChanged 方法 , 在該方法的第二個參數 int i , 就是被點擊的 RadioBtton 的索引 ;

代碼示例 :

package com.example.radiobutton.slice;import com.example.radiobutton.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Button; import ohos.agp.components.RadioContainer; 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);// 獲取 RadioContainerRadioContainer radioContainer = (RadioContainer) findComponentById(ResourceTable.Id_radioContainer);radioContainer.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {@Overridepublic void onCheckedChanged(RadioContainer radioContainer, int i) {text.setText("當前選中 : " + i);}});}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);} }

布局文件 :

<?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"><!-- 單選按鈕容器 --><RadioContainerohos:id="$+id:radioContainer"ohos:height="match_content"ohos:width="match_parent"ohos:orientation="vertical"><RadioButtonohos:id="$+id:radioButton1"ohos:height="match_content"ohos:width="match_content"ohos:layout_alignment="horizontal_center"ohos:text="單選按鈕 0"ohos:text_size="100"/><RadioButtonohos:id="$+id:radioButton2"ohos:height="match_content"ohos:width="match_content"ohos:layout_alignment="horizontal_center"ohos:text="單選按鈕 1"ohos:text_size="100"/><RadioButtonohos:id="$+id:radioButton3"ohos:height="match_content"ohos:width="match_content"ohos:layout_alignment="horizontal_center"ohos:text="單選按鈕 2"ohos:text_size="100"/></RadioContainer><Textohos:id="$+id:text"ohos:height="match_content"ohos:width="match_content"ohos:layout_alignment="horizontal_center"ohos:text="當前選中 : "ohos:text_size="100"ohos:text_color="#FF0000"/></DirectionalLayout>

執行結果 :





三、GitHub 地址



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

RadioButton 與 RadioContainer 組件示例 Module : https://github.com/han1202012/HarmonyHelloWorld/tree/master/radiobutton

總結

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

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