您的位置 首页 > 资格资历

radiobutton,自学鸿蒙应用开发

这篇文章给大家聊聊关于radiobutton,以及自学鸿蒙应用开发对应的知识点,希望对各位有所帮助,不要忘了收藏本站哦。

本文介绍在鸿蒙应用中RadioButton和RadioContainer组件的基本用法。

增加RadioButton和RadioContainer组件

如下代码中46行~66行所示,在布局中增加RadioButton和RadioContainer组件。

<?xmlversion="1.0"encoding="utf-8"?>\n<DirectionalLayout\nxmlns:ohos="http://schemas.huawei.com/res/ohos"\nohos:height="match_parent"\nohos:width="match_parent"\nohos:orientation="vertical">\n<Component\nohos:height="0vp"\nohos:weight="3"\nohos:width="match_parent"\n/>\n<DirectionalLayout\nxmlns:ohos="http://schemas.huawei.com/res/ohos"\nohos:height="match_content"\nohos:width="match_content"\nohos:layout_alignment="center"\nohos:orientation="vertical">\n<Image\nohos:id="$+id:image"\nohos:width="match_content"\nohos:height="match_content"\nohos:layout_alignment="center"\nohos:image_src="$media:DevEco"\n/>\n<TextField\nohos:id="$+id:text_field"\nohos:width="match_parent"\nohos:height="30vp"\nohos:text_size="20fp"\nohos:text_alignment="center"\nohos:hint="Pleaseinputtextandpress[Clickme!]button."\nohos:background_element="$graphic:background_text_field"\n/>\n<Button\nohos:id="$+id:hello_button"\nohos:width="match_content"\nohos:height="match_content"\nohos:text_size="27fp"\nohos:text="Clickme!"\nohos:layout_alignment="center"\nohos:background_element="$graphic:background_button"\nohos:margin="15vp"\nohos:right_padding="8vp"\nohos:left_padding="8vp"\n/>\n<RadioContainer\nohos:id="$+id:radio_container"\nohos:height="match_content"\nohos:width="match_content"\nohos:top_margin="32vp"\nohos:layout_alignment="horizontal_center"\nohos:orientation="horizontal">\n<RadioButton\nohos:id="$+id:r24h"\nohos:height="40vp"\nohos:width="match_content"\nohos:text="24H"\nohos:marked="true"\nohos:text_size="20fp"/>\n<RadioButton\nohos:id="$+id:r12h"\nohos:height="40vp"\nohos:width="match_content"\nohos:text="12H"\nohos:text_size="20fp"/>\n</RadioContainer>\n<TimePicker\nohos:id="$+id:time_picker"\nohos:24_hour_mode="false"\nohos:height="match_content"\nohos:width="match_parent"\nohos:layout_alignment="horizontal_center"\nohos:text_am="AM"\nohos:text_pm="PM"\nohos:normal_text_size="20fp"\nohos:selected_text_size="25fp"/>\n</DirectionalLayout>\n<Component\nohos:height="0vp"\nohos:weight="5"\nohos:width="match_parent"\n/>\n</DirectionalLayout>

代码中组件id被指定为radio_container,会在下面的响应代码中用到。

在代码中使用RadioButton和RadioContainer组件

下面代码中的第18行获取RadioContainer组件后,在第22行根据RadioContainer的状态更新TimePicker的形式,然后在第40行~43行为RadioContainer增加响应处理,其内容是同样是根据选中的RadioButton的索引更新TimePicker的形式。

importohos.aafwk.ability.AbilitySlice;\nimportohos.aafwk.content.Intent;\nimportohos.agp.components.*;\nimportohos.agp.window.dialog.ToastDialog;\nimportjava.time.LocalTime;\nimportjava.time.temporal.ChronoUnit;\n\npublicclassComponentAbilitySliceextendsAbilitySlice{\n@Override\npublicvoidonStart(Intentintent){\nsuper.onStart(intent);\nsuper.setUIContent(ResourceTable.Layout_ability_component);\n//获取textfield输入组件\nTextFieldtf=(TextField)findComponentById(ResourceTable.Id_text_field);\n//获取button组件\nButtonbutton=(Button)findComponentById(ResourceTable.Id_hello_button);\n//获取RedioContainer组件\nRadioContainerrcontainer=(RadioContainer)findComponentById(ResourceTable.Id_radio_container);\n//获取TimePicker组件\nTimePickerpicker=(TimePicker)findComponentById(ResourceTable.Id_time_picker);\n//根据RadioContainer的状态设定TimePicker的形式。\npicker.set24Hour(rcontainer.getMarkedButtonId()==0);\n//为按钮设置点击事件回调\nbutton.setClickedListener(newComponent.ClickedListener(){\npublicvoidonClick(Componentv){\nLocalTimerightNow=LocalTime.now();\nLocalTimepickTime=LocalTime.of(picker.getHour(),picker.getMinute(),picker.getSecond());\nStringmsg;\nif(pickTime.isBefore(rightNow))\nmsg="所选时间比现在时刻早"+pickTime.until(rightNow,ChronoUnit.SECONDS)+"秒";\nelseif(pickTime.isAfter(rightNow))\nmsg="所选时间比现在时刻晚"+rightNow.until(pickTime,ChronoUnit.SECONDS)+"秒";\nelse\nmsg="所选时间和现在时刻一样";\nnewToastDialog(getContext())\n.setText(msg)\n.show();\n}\n});\n//为RadioContainer设置事件响应\nrcontainer.setMarkChangedListener((radioContainer1,index)->{\npicker.set24Hour(index==0);\n});\n//为TimePicker设定事件响应\npicker.setTimeChangedListener(\nnewTimePicker.TimeChangedListener(){\n@Override\npublicvoidonTimeChanged(TimePickertimePicker,inthour,intminute,intsecond){\ntf.setText(hour+":"+minute+":"+second);\n}\n}\n);\n}\n@Override\npublicvoidonActive(){\nsuper.onActive();\n}\n@Override\npublicvoidonForeground(Intentintent){\nsuper.onForeground(intent);\n}\n}

画面显示如下:

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-component-radiocontainer-0000001063470429

https://developer.harmonyos.com/cn/docs/documentation/doc-references/radiocontainer-0000001054678720

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-component-radiobutton-0000001060647792

https://developer.harmonyos.com/cn/docs/documentation/doc-references/radiobutton-0000001054518732

《实战Python设计模式》是作者最近出版的新书,拜托多多关注!

本书利用Python的标准GUI工具包tkinter,通过可执行的示例对23个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习PythonGUI编程的读者可以将本书中的示例作为设计和开发的参考;使用Python语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。

觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

关于本次radiobutton和自学鸿蒙应用开发的问题分享到这里就结束了,如果解决了您的问题,我们非常高兴。

本站涵盖的内容、图片、视频等数据,部分未能与原作者取得联系。若涉及版权问题,请及时通知我们并提供相关证明材料,我们将及时予以删除!谢谢大家的理解与支持!

Copyright © 2023