效果:
代码:
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 "";
}
}
}