19 - 多选框练习

本文档展示了如何使用OHOS GUI组件创建一个包含钟表、复选框和按钮的界面,用于自定义时间显示格式。用户可以选择年、月、日、小时、分钟和秒,然后通过点击"确定"按钮,使用自定义的吐司对话框显示所选时间。代码包括XML布局文件、自定义吐司样式和事件处理函数。
摘要由CSDN通过智能技术生成

效果:

代码:

ability_main.xml

<?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:background_element="#778495"
        ohos:mode_24_hour="yyyy年MM月dd日 HH:mm:ss"
        ohos:text_color="#FFF"
        ohos:text_size="30vp"
        />

    <Checkbox
        ohos:id="$+id:year"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="#5E5CA2"
        ohos:layout_alignment="left"
        ohos:left_margin="30vp"
        ohos:text="年"
        ohos:text_size="30vp"
        ohos:top_margin="30vp"
        />

    <Checkbox
        ohos:id="$+id:month"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="#5E5CA2"
        ohos:layout_alignment="left"
        ohos:left_margin="30vp"
        ohos:text="月"
        ohos:text_size="30vp"
        ohos:top_margin="10vp"
        />

    <Checkbox
        ohos:id="$+id:day"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="#5E5CA2"
        ohos:layout_alignment="left"
        ohos:left_margin="30vp"
        ohos:text="日"
        ohos:text_size="30fp"
        ohos:top_margin="10vp"
        />

    <Checkbox
        ohos:id="$+id:hour"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="#5E5CA2"
        ohos:layout_alignment="left"
        ohos:left_margin="30vp"
        ohos:text="时"
        ohos:text_size="30fp"
        ohos:top_margin="10vp"
        />

    <Checkbox
        ohos:id="$+id:minutes"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="#5E5CA2"
        ohos:layout_alignment="left"
        ohos:left_margin="30vp"
        ohos:text="分"
        ohos:text_size="30fp"
        ohos:top_margin="10vp"
        />

    <Checkbox
        ohos:id="$+id:second"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="#5E5CA2"
        ohos:layout_alignment="left"
        ohos:left_margin="30vp"
        ohos:text="秒"
        ohos:text_size="30fp"
        ohos:top_margin="10vp"
        />

    <Button
        ohos:id="$+id:submit"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="#FF0000"
        ohos:text="确定"
        ohos:text_color="#000000"
        ohos:text_size="30fp"
        ohos:top_margin="10vp"
        />

</DirectionalLayout>

mytoast.xml

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:orientation="vertical">


    <Text
        ohos:id="$+id:msg"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text_size="30fp"
        ohos:text_color="#FFFFFF"
        ohos:text_alignment="center"
        ohos:background_element="#464343"/>

</DirectionalLayout>

ToastUtils.java

package com.example.myapplication.ToastUtils;

import com.example.myapplication.ResourceTable;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

public class ToastUtils {
    public static void showDialog(Context context,String message){

        //1.把xml文件加载到内存当中。
        DirectionalLayout dl = (DirectionalLayout) LayoutScatter.getInstance(context).parse(ResourceTable.Layout_mytoast, null, false);

        //2.获取到当前布局对象中文本组件
        Text msg = (Text) dl.findComponentById(ResourceTable.Id_msg);
        //3.把需要提示的信息设置到文本组件中
        msg.setText(message);

        //4.创建一个吐司对象
        ToastDialog td = new ToastDialog(context);
        //设置吐司的大小。--- 默认是包裹内容
        td.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT,DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        //设置出现的时间
        td.setDuration(2000);
        //设置对齐方式
        td.setAlignment(LayoutAlignment.BOTTOM);
        //把xml中的布局对象交给吐司
        td.setContentCustomComponent(dl);
        //把吐司做一个偏移
        //偏移是以吐司弹框的基准位置进行偏移的
        //如果是正数,就默认往屏幕中央去偏移
        //如果是负数,就往屏幕中央的反方向去偏移
        td.setOffset(0,200);
        //让吐司出现
        td.show();
    }
}

MainAbilitySlice.java

package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import com.example.myapplication.ToastUtils.ToastUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    Button but;
    Clock clock;

    @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 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, "hh:");
        String second = check(ResourceTable.Id_second, "ss");

        //yyyy年MM月dd日 HH:mm: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 "";
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值