> 文档中心 > 2021-08-19 HarmonyOS实战 Checkbox的使用

2021-08-19 HarmonyOS实战 Checkbox的使用

<?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"><Clock    ohos:id="$+id:clock"    ohos:height="match_content"    ohos:width="match_content"    ohos:text_color="#FFFFFF"    ohos:text_size="30vp"    ohos:background_element="#778495"    ohos:mode_24_hour="yyyy年MM月dd日 HH:mm:ss"/><Checkbox    ohos:id="$+id:year"    ohos:height="match_content"    ohos:width="match_content"    ohos:text="年"    ohos:text_size="30fp"    ohos:layout_alignment="left"    ohos:left_margin="30vp"    ohos:top_margin="30vp"    ohos:background_element="#5E5CA2"/>    <Checkbox ohos:id="$+id:month" ohos:height="match_content" ohos:width="match_content" ohos:text="月" ohos:text_size="30fp" ohos:layout_alignment="left" ohos:left_margin="30vp" ohos:top_margin="10vp" ohos:background_element="#5E5CA2"/>    <Checkbox ohos:id="$+id:day" ohos:height="match_content" ohos:width="match_content" ohos:text="日" ohos:text_size="30fp" ohos:layout_alignment="left" ohos:left_margin="30vp" ohos:top_margin="10vp" ohos:background_element="#5E5CA2"/>    <Checkbox ohos:id="$+id:hour" ohos:height="match_content" ohos:width="match_content" ohos:text="时" ohos:text_size="30fp" ohos:layout_alignment="left" ohos:left_margin="30vp" ohos:top_margin="10vp" ohos:background_element="#5E5CA2"/>    <Checkbox ohos:id="$+id:minutes" ohos:height="match_content" ohos:width="match_content" ohos:text="分" ohos:text_size="30fp" ohos:layout_alignment="left" ohos:left_margin="30vp" ohos:top_margin="10vp" ohos:background_element="#5E5CA2"/>    <Checkbox ohos:id="$+id:second" ohos:height="match_content" ohos:width="match_content" ohos:text="秒" ohos:text_size="30fp" ohos:layout_alignment="left" ohos:left_margin="30vp" ohos:top_margin="10vp" ohos:background_element="#5E5CA2"/>    <Button ohos:id="$+id:submit" ohos:height="match_content" ohos:width="match_content" ohos:text="确定" ohos:top_margin="10vp" ohos:text_size="30fp" ohos:text_color="#000000" ohos:background_element="#FF0000"/>
package com.example.checkboxapplication2.slice;import com.example.checkboxapplication2.ResourceTable;import com.example.checkboxapplication2.toastutils.ToastUtils;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.Button;import ohos.agp.components.Checkbox;import ohos.agp.components.Clock;import ohos.agp.components.Component;public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {    Clock clock;    Button but;    @Override    public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); //1.找到组件 clock = (Clock) findComponentById(ResourceTable.Id_clock); but = (Button) findComponentById(ResourceTable.Id_submit); //2.给按钮去绑定单击事件 but.setClickedListener(this);    }    @Override    public void onActive() { super.onActive();    }    @Override    public void onForeground(Intent intent) { super.onForeground(intent);    }    @Override    public void onClick(Component component) { //最简单的实现方式 //判断6个多选框是否被选中,如果被选中,再拼接时间格式就可以了 String year = check(ResourceTable.Id_year,"yyyy年"); String month = check(ResourceTable.Id_month,"MM月"); String day = check(ResourceTable.Id_day,"dd日"); String hour = check(ResourceTable.Id_hour,"HH:"); String minutes = check(ResourceTable.Id_minutes,"mm:"); String second = check(ResourceTable.Id_second,"ss"); //拼接操作 String result = year + month + day + " " + hour + minutes + second; // " " if(result.length() == 1){     //表示所有的多选框都没有选择     ToastUtils.showDialog(this,"不能一个都不选");     //细节:表示当代码弹出吐司之后,方法就执行完毕,下面的代码不会执行了。     return; } //把时间展示的格式交给Clock组件 clock.setFormatIn24HourMode(result);    }    //定义一个方法    //作用:判断多选框有没有被选中    //如果被选中了,则返回字符串    //如果没有被选中,则返回一个长度为0的字符串    public String check(int id,String result){ //根据传递过来的id找到对应的多选框 Checkbox cb = (Checkbox) findComponentById(id); //判断当前的多选框有没有被选中 //如果被选中,就返回result //如果没有被选中,就返回"" if(cb.isChecked()){     return result; }else{     return ""; }    }}