自學(xué)鴻蒙應(yīng)用開(kāi)發(fā)(38)- 修改RadioButton默認(rèn)動(dòng)作
鴻蒙系統(tǒng)中RadionButton的默認(rèn)動(dòng)作是點(diǎn)擊某個(gè)選項(xiàng)時(shí)出現(xiàn)選中標(biāo)記,再次點(diǎn)擊時(shí)取消選中狀態(tài),表現(xiàn)如下面視頻中的第一排RadioButton。有時(shí)可能希望再次點(diǎn)擊時(shí)可以維持選中狀態(tài),就像下面視頻中第二排RadioButton那樣。
布局文件
文件中簡(jiǎn)單地放置了兩個(gè)包含3個(gè)RadioButton的RadioContainer。
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><RadioContainerohos:id="$+id:radio_container1"ohos:height="match_content"ohos:width="match_content"ohos:top_margin="32vp"ohos:orientation="horizontal"ohos:layout_alignment="horizontal_center"><RadioButtonohos:id="$+id:radio_button_1"ohos:height="40vp"ohos:width="match_content"ohos:text="A.Learning"ohos:text_size="14fp"/><RadioButtonohos:id="$+id:radio_button_2"ohos:height="40vp"ohos:width="match_content"ohos:text="B.Innovation"ohos:text_size="14fp"/><RadioButtonohos:id="$+id:radio_button_3"ohos:height="40vp"ohos:width="match_content"ohos:text="C.Benefit"ohos:text_size="14fp"/></RadioContainer><RadioContainerohos:id="$+id:radio_container2"ohos:height="match_content"ohos:width="match_content"ohos:top_margin="32vp"ohos:orientation="horizontal"ohos:layout_alignment="horizontal_center"><RadioButtonohos:id="$+id:radio_huawei"ohos:height="60vp"ohos:width="match_content"ohos:text="H.Huawei"ohos:text_size="14fp"/><RadioButtonohos:id="$+id:radio_xiaomi"ohos:height="60vp"ohos:width="match_content"ohos:text="X.Xiaomi"ohos:text_size="14fp"/><RadioButtonohos:id="$+id:radio_oppo"ohos:height="60vp"ohos:width="match_content"ohos:text="O.Oppo"ohos:text_size="14fp"/></RadioContainer></DirectionalLayout>
修改RadionButton的行為
下面的代碼在RadioButton的選中狀態(tài)發(fā)生變化時(shí)修改按鈕是否接受單擊的屬性。
public class MainAbilitySlice extends AbilitySlice {public void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);AbsButton.CheckedStateChangedListener listener = new AbsButton.CheckedStateChangedListener() {public void onCheckedChanged(AbsButton absButton, boolean b) {absButton.setClickable(!b);}};RadioButton rb1 = (RadioButton)findComponentById(ResourceTable.Id_radio_huawei);rb1.setCheckedStateChangedListener(listener);RadioButton rb2 = (RadioButton)findComponentById(ResourceTable.Id_radio_xiaomi);rb2.setCheckedStateChangedListener(listener);RadioButton rb3 = (RadioButton)findComponentById(ResourceTable.Id_radio_oppo);rb3.setCheckedStateChangedListener(listener);}}
參考代碼
完整代碼可以從以下鏈接下載:
https://github.com/xueweiguo/Harmony/tree/master/RadioButtonTest
作者著作介紹
《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者去年3月份出版的技術(shù)書(shū)籍,該書(shū)利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過(guò)可執(zhí)行的示例對(duì)23 個(gè)設(shè)計(jì)模式逐個(gè)進(jìn)行說(shuō)明。這樣一方面可以使讀者了解真實(shí)的軟件開(kāi)發(fā)工作中每個(gè)設(shè)計(jì)模式的運(yùn)用場(chǎng)景和想要解決的問(wèn)題;另一方面通過(guò)對(duì)這些問(wèn)題的解決過(guò)程進(jìn)行說(shuō)明,讓讀者明白在編寫(xiě)代碼時(shí)如何判斷使用設(shè)計(jì)模式的利弊,并合理運(yùn)用設(shè)計(jì)模式。

對(duì)設(shè)計(jì)模式感興趣而且希望隨學(xué)隨用的讀者通過(guò)本書(shū)可以快速跨越從理解到運(yùn)用的門(mén)檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書(shū)中的示例作為設(shè)計(jì)和開(kāi)發(fā)的參考;使用Python 語(yǔ)言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書(shū)中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺(jué)得本文有幫助?請(qǐng)分享給更多人。
關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_(kāi)發(fā),面向?qū)ο笏伎迹?/span>
