前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自学鸿蒙应用开发(12)- Checkbox

自学鸿蒙应用开发(12)- Checkbox

作者头像
面向对象思考
发布2021-01-13 11:19:04
4030
发布2021-01-13 11:19:04
举报
文章被收录于专栏:C++核心准则原文翻译

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

增加Checkbox组件

如下代码中47行~52行所示,在布局中增加Checkbox组件。

代码语言:javascript
复制
<?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"
            />
        <Checkbox
            ohos:id="$+id:check_box"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:layout_alignment="center"
            ohos:text="24小时制"
            ohos:text_size="20fp" />
        <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被指定为check_box,会在下面的响应代码中用到。

在代码中使用Checkbox组件

下面代码中的第20行获取RadioContainer组件后,在第24行根据Checkbox的状态更新TimePicker的形式,然后在第42行~45行为Checkbox增加响应处理,其内容是同样是根据Checkbox的状态更新TimePicker的形式。

代码语言:javascript
复制
package com.example.helloharmony.slice;
import com.example.helloharmony.ResourceTable;
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);
        //获取Checkbox组件
        Checkbox check_box = (Checkbox)findComponentById(ResourceTable.Id_check_box);
        //获取TimePicker组件
        TimePicker picker = (TimePicker) findComponentById(ResourceTable.Id_time_picker);
        //根据Checkbox的状态设定TimePicker的形式。
        picker.set24Hour(check_box.isChecked());
        // 为按钮设置点击事件回调
        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设置事件响应
        check_box.setCheckedStateChangedListener((view, state) -> {
            picker.set24Hour(state);
        });
        //为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);
    }
}
代码语言:javascript
复制

参考文档

Checkbox组件

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

Checkbox类

https://developer.harmonyos.com/cn/docs/documentation/doc-references/checkbox-0000001054558715

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-01-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档