自學(xué)鴻蒙應(yīng)用開發(fā)(38)- 修改RadioButton默認動作
鴻蒙系統(tǒng)中RadionButton的默認動作是點擊某個選項時出現(xiàn)選中標(biāo)記,再次點擊時取消選中狀態(tài),表現(xiàn)如下面視頻中的第一排RadioButton。有時可能希望再次點擊時可以維持選中狀態(tài),就像下面視頻中第二排RadioButton那樣。
布局文件
文件中簡單地放置了兩個包含3個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ā)生變化時修改按鈕是否接受單擊的屬性。
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
作者著作介紹
《實戰(zhàn)Python設(shè)計模式》是作者去年3月份出版的技術(shù)書籍,該書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設(shè)計模式逐個進行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設(shè)計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設(shè)計模式的利弊,并合理運用設(shè)計模式。

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