先,在一个Form窗口中,我们可以定义3个radiobutton,radioButton1、radioButton2和radioButton3,以及button1和button2(这里可以是其他控件)。
为了实现单击radioButton1时,button1显示"A1",button2显示"A2";
单击radioButton2时,button1显示"B1",button2显示"B2";
单击radioButton3时,button1显示"C1",button2显示"C2".
下面的代码就可以实现这个功能:
文介绍在鸿蒙应用中RadioButton和RadioContainer组件的基本用法。
<script src="https://lf6-cdn-tos.bytescm.com/obj/cdn-static-resource/tt_player/tt.player.js?v=20160723"></script>
增加RadioButton和RadioContainer组件
如下代码中46行~66行所示,在布局中增加RadioButton和RadioContainer组件。
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Component
ohos:height="0vp"
ohos:weight="3"
ohos:width="match_parent"
/>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_content"
ohos:width="match_content"
ohos:layout_alignment="center"
ohos:orientation="vertical">
<Image
ohos:id="$+id:image"
ohos:width="match_content"
ohos:height="match_content"
ohos:layout_alignment="center"
ohos:image_src="$media:DevEco"
/>
<TextField
ohos:id="$+id:text_field"
ohos:width="match_parent"
ohos:height="30vp"
ohos:text_size="20fp"
ohos:text_alignment="center"
ohos:hint="Please input text and press [Click me!] button."
ohos:background_element="$graphic:background_text_field"
/>
<Button
ohos:id="$+id:hello_button"
ohos:width="match_content"
ohos:height="match_content"
ohos:text_size="27fp"
ohos:text="Click me!"
ohos:layout_alignment="center"
ohos:background_element="$graphic:background_button"
ohos:margin="15vp"
ohos:right_padding="8vp"
ohos:left_padding="8vp"
/>
<RadioContainer
ohos:id="$+id:radio_container"
ohos:height="match_content"
ohos:width="match_content"
ohos:top_margin="32vp"
ohos:layout_alignment="horizontal_center"
ohos:orientation="horizontal">
<RadioButton
ohos:id="$+id:r24h"
ohos:height="40vp"
ohos:width="match_content"
ohos:text="24H"
ohos:marked="true"
ohos:text_size="20fp"/>
<RadioButton
ohos:id="$+id:r12h"
ohos:height="40vp"
ohos:width="match_content"
ohos:text="12H"
ohos:text_size="20fp"/>
</RadioContainer>
<TimePicker
ohos:id="$+id:time_picker"
ohos:24_hour_mode="false"
ohos:height="match_content"
ohos:width="match_parent"
ohos:layout_alignment="horizontal_center"
ohos:text_am="AM"
ohos:text_pm="PM"
ohos:normal_text_size="20fp"
ohos:selected_text_size="25fp"/>
</DirectionalLayout>
<Component
ohos:height="0vp"
ohos:weight="5"
ohos:width="match_parent"
/>
</DirectionalLayout>
代码中组件id被指定为radio_container,会在下面的响应代码中用到。
在代码中使用RadioButton和RadioContainer组件
下面代码中的第18行获取RadioContainer组件后,在第22行根据RadioContainer的状态更新TimePicker的形式,然后在第40行~43行为RadioContainer增加响应处理,其内容是同样是根据选中的RadioButton的索引更新TimePicker的形式。
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class ComponentAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_component);
//获取textfield输入组件
TextField tf=(TextField) findComponentById(ResourceTable.Id_text_field);
//获取button组件
Button button=(Button) findComponentById(ResourceTable.Id_hello_button);
//获取RedioContainer组件
RadioContainer rcontainer=(RadioContainer)findComponentById(ResourceTable.Id_radio_container);
//获取TimePicker组件
TimePicker picker=(TimePicker) findComponentById(ResourceTable.Id_time_picker);
//根据RadioContainer的状态设定TimePicker的形式。
picker.set24Hour(rcontainer.getMarkedButtonId()==0);
// 为按钮设置点击事件回调
button.setClickedListener(new Component.ClickedListener() {
public void onClick(Component v) {
LocalTime rightNow=LocalTime.now();
LocalTime pickTime=LocalTime.of(picker.getHour(), picker.getMinute(), picker.getSecond());
String msg;
if(pickTime.isBefore(rightNow))
msg="所选时间比现在时刻早" + pickTime.until(rightNow, ChronoUnit.SECONDS) + "秒";
else if(pickTime.isAfter(rightNow))
msg="所选时间比现在时刻晚" + rightNow.until(pickTime, ChronoUnit.SECONDS) + "秒";
else
msg="所选时间和现在时刻一样";
new ToastDialog(getContext())
.setText(msg)
.show();
}
});
//为RadioContainer设置事件响应
rcontainer.setMarkChangedListener((radioContainer1, index) -> {
picker.set24Hour(index==0);
});
//为TimePicker设定事件响应
picker.setTimeChangedListener(
new TimePicker.TimeChangedListener() {
@Override
public void onTimeChanged(TimePicker timePicker, int hour, int minute, int second) {
tf.setText(hour + ":" + minute + ":" + second);
}
}
);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
画面显示如下:
参考文档
RadioContainer组件:
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-component-radiocontainer-0000001063470429
RadioContainer类:
https://developer.harmonyos.com/cn/docs/documentation/doc-references/radiocontainer-0000001054678720
RadioButton组件
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-component-radiobutton-0000001060647792
RadioButton类
https://developer.harmonyos.com/cn/docs/documentation/doc-references/radiobutton-0000001054518732
新书介绍
《实战Python设计模式》是作者最近出版的新书,拜托多多关注!
本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。
对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!
篇文章主要的向大家介绍了关于html input标签的单选按钮的使用方法,还有关于HTML input标签的单选默认按钮的做法。接下来我们一起来看看这篇文章吧
<input> 标签用于搜集用户信息。根据不同的type属性值,输入字段拥有很多种形式。输入字段可以是文本字段、复选框、掩码后的文本控件、单选按钮、按钮等等。
> <form action="form_action.asp" method="get">
>
> <input type="radio" name="radio" value="1">单选1
>
> <input type="radio" name="radio" value="2">单选2
>
> <input type="radio" name="radio" value="3">单选3
>
> <input type="radio" name="radio" value="4">单选4
>
> </form>
这个的效果很容易看到,我们还是先看看浏览器中的显示效果吧:
这个效果一眼就能看到,很简单的一个代码
还有种是很多网站上都是经常见到的,比如:单选性别,这个基本上都是用这种单选框去制作的。代码如下:
HTML中的单选按钮实现男女性别选择,不让男女同是都能都能选择,实现方法:在按钮的属性里写一个name属性,并且把name的值设置成相同的
> <input id="man" type="radio" checked="checked" name="1" />男
>
> <input id="woman" type="radio" name="1"/>女
这个就不给图了,比上面那个还简单,就两个单选框,我们经常遇到的这个。
现在来说说HTML单选框按钮怎么默认选中:
首先我们先把第一个实例拿出来继续说,我们只需要在其中加一个属性,如下:
> <form action="form_action.asp" method="get">
>
> <input type="radio" name="radio" value="1">单选1
>
> <input type="radio" name="radio" value="2" checked>单选2
>
> <input type="radio" name="radio" value="3">单选3
>
> <input type="radio" name="radio" value="4">单选4
>
> </form>
这上面我没做任何的点击,自己出现在那上面的,刷新过后还能看到在单选2上面。
我们就可以看到,这样就把单选框给默认选中了,大家可以自己试试,多敲敲代码。
好了,以上就是这篇关于html input标签做单选按钮的文章了,有问题的可以在下方提问。
以上就是html单选按钮默认选中怎么做?input标签的单选按钮用法实例的详细内容,更多请关注我!!!
我自己是一名从事了多年开发的web前端老程序员,目前辞职在做自己的web前端私人定制课程,今年我花了一个月整理了一份最适合2020年学习的web前端学习干货,各种框架都有整理,送给每一位前端小伙伴,想要获取的可以关注我的头条号并在后台私信我:前端,即可免费获取。
*请认真填写需求信息,我们会在24小时内与您取得联系。