当前位置:网站首页>鸿蒙 Picker日期选择器实现教程
鸿蒙 Picker日期选择器实现教程
2022-06-09 15:14:00 【华为云】
前言 :
各位同学大家好,有段时间没有给大家更新文章了,具体多久呢我也记不清楚了。最近又去鸿蒙开发社区看了一看 有get一些新知识点 我觉得有必要给大家分享下 今天就给大家分享一个Picker 配合CommonDialog 实现的 日期选择器
效果图


具体实现
package com.example.picker.dialog;import com.example.picker.ResourceTable;import com.example.picker.listener.PickerdialogListener;import ohos.aafwk.ability.Ability;import ohos.agp.components.Button;import ohos.agp.components.Component;import ohos.agp.components.LayoutScatter;import ohos.agp.components.Picker;import ohos.agp.window.dialog.CommonDialog;import ohos.app.Context;/*** * 创建人: xuqing * 创建时间:2021年3月19日15:18:33 * 类说明:底部选择器弹窗 * */public class PickerDialog extends CommonDialog implements Component.ClickedListener { private Ability context; private PickerdialogListener listener; private Picker picker ; private Button cancelbtn,affirmbtn; private String getdata; private String[]getStr={"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"}; Component layout; public PickerDialog(Ability context, PickerdialogListener listener) { super(context); this.context=context; this.listener=listener; } @Override protected void onCreate() { super.onCreate(); layout = LayoutScatter.getInstance(context). parse(ResourceTable.Layout_common_dialog, null, true); setTransparent(true); setContentCustomComponent(layout); initview(); } private void initview() { picker= (Picker)layout.findComponentById(ResourceTable.Id_test_picker); cancelbtn= (Button) layout.findComponentById(ResourceTable.Id_cancel_btn); affirmbtn= (Button) layout.findComponentById(ResourceTable.Id_affirm_btn); cancelbtn.setClickedListener(this); affirmbtn.setClickedListener(this); picker.setDisplayedData(getStr); picker.setValueChangedListener(new Picker.ValueChangedListener() { @Override public void onValueChanged(Picker picker, int i, int i1) { System.out.println("i -- > "+i+" i1 --- > "+ i1); System.out.println(getStr[i1]); } }); picker.setValueChangedListener((picker1, oldVal, newVal) -> { // oldVal:上一次选择的值; newVal:最新选择的值 System.out.println("oldVal "+oldVal +"newVal --- > "+newVal); System.out.println("getstr --- > "+getStr[newVal]); getdata=getStr[newVal]; }); picker.setFormatter(i -> { String value; switch (i) { case 0: value = "Mon"; break; case 1: value = "Tue"; break; case 2: value = "Wed"; break; case 3: value = "Thu"; break; case 4: value = "Fri"; break; case 5: value = "Sat"; break; case 6: value = "Sun"; break; default: value = "" + i; } return value; }); } @Override public void onClick(Component component) { switch (component.getId()){ case ResourceTable.Id_affirm_btn: PickerDialog.this.hide(); listener.getPickerStrSuccess(getdata); break; case ResourceTable.Id_cancel_btn: PickerDialog.this.hide(); listener.getPickerStrerror(); break; default: break; } }}PickerDialog 继承sdk的 CommonDialog 并重写 oncreate 方法 和自己的构造方法 然后我们加载了PickerDialog 的布局文件
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="300vp" ohos:width="match_parent" ohos:horizontal_center="true" ohos:vertical_center="true" ohos:orientation="vertical" > <DirectionalLayout ohos:height="50vp" ohos:width="match_parent" ohos:orientation="horizontal" > <Button ohos:id="$+id:cancel_btn" ohos:height="match_parent" ohos:width="0vp" ohos:weight="1" ohos:text="取消" ohos:text_size="20vp" ohos:text_color="#FF171818" > </Button> <Button ohos:id="$+id:affirm_btn" ohos:height="match_parent" ohos:width="0vp" ohos:weight="1" ohos:text="确定" ohos:text_size="20vp" ohos:text_color="#FF171818" > </Button> </DirectionalLayout> <Picker ohos:id="$+id:test_picker" ohos:height="match_parent" ohos:width="match_parent" ohos:background_element="#E1FFFF" ohos:vertical_center="true" ohos:horizontal_center="true" ohos:normal_text_size="16fp" ohos:selected_text_size="16fp"/> </DirectionalLayout>
这边我们写了2个button 和一个picker 选择器组件 我们在PickerDialog 中填充我们的布局文件 然后初始化我们的控件
- 加载布局
@Override protected void onCreate() { super.onCreate(); layout = LayoutScatter.getInstance(context). parse(ResourceTable.Layout_common_dialog, null, true); setTransparent(true); setContentCustomComponent(layout); initview(); }- 初始化控件
picker= (Picker)layout.findComponentById(ResourceTable.Id_test_picker); cancelbtn= (Button) layout.findComponentById(ResourceTable.Id_cancel_btn); affirmbtn= (Button) layout.findComponentById(ResourceTable.Id_affirm_btn); cancelbtn.setClickedListener(this); affirmbtn.setClickedListener(this);- 选择器添加数据
private String[]getStr={"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"}; picker.setDisplayedData(getStr);- 响应选择器变化
picker.setValueChangedListener((picker1, oldVal, newVal) -> { // oldVal:上一次选择的值; newVal:最新选择的值 System.out.println("oldVal "+oldVal +"newVal --- > "+newVal); System.out.println("getstr --- > "+getStr[newVal]); getdata=getStr[newVal]; });- 格式化Picker的显示 通过Picker的setFormatter(Formatter formatter)方法,用户可以将Picker选项中显示的字符串修改为特定的格式。
picker.setFormatter(i -> { String value; switch (i) { case 0: value = "Mon"; break; case 1: value = "Tue"; break; case 2: value = "Wed"; break; case 3: value = "Thu"; break; case 4: value = "Fri"; break; case 5: value = "Sat"; break; case 6: value = "Sun"; break; default: value = "" + i; } return value; });在MainAbility 中显示
package com.example.picker;import com.example.picker.dialog.PickerDialog;import com.example.picker.listener.PickerdialogListener;import com.example.picker.slice.MainAbilitySlice;import ohos.aafwk.ability.Ability;import ohos.aafwk.content.Intent;import ohos.agp.components.Button;import ohos.agp.components.Component;import ohos.agp.components.TableLayout;import ohos.agp.components.Text;import static ohos.agp.utils.LayoutAlignment.BOTTOM;public class MainAbility extends Ability { private Button button; private Text text; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); initview(); } private void initview() { button= (Button) findComponentById(ResourceTable.Id_mainbtn); text= (Text) findComponentById(ResourceTable.Id_miantext); if(button!=null){ button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { PickerDialog dialog=new PickerDialog(MainAbility.this, new PickerdialogListener() { @Override public void getPickerStrSuccess(String str) { text.setText(str); } @Override public void getPickerStrerror() { } }); dialog.setAlignment(BOTTOM); dialog.show(); } }); } }}MainAbility 布局文件
<?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"> <Button ohos:id="$+id:mainbtn" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="center" ohos:text="点击打开弹窗" ohos:text_size="15fp" ohos:top_margin="20vp" ohos:background_element="$graphic:background_ability_main" > </Button> <Text ohos:id="$+id:miantext" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="center" ohos:top_margin="20vp" ohos:text="哈哈哈" ohos:text_size="15fp" > </Text></DirectionalLayout>到此鸿蒙 Picker日期选择器实现教程 就讲完了。
最后总结
鸿蒙里面提供了picker选择器控件给我们用 所以我们配合sdk提供的 CommonDialog 弹窗控件 就很容易实现比较简单的底部的日期选择器 。整个功能实现比较接近原生安卓 但是又比安卓更进一步所以 实现起来不难 有兴趣的同学还可以尝试实现更为复杂多样的选择 这边就不展开讲了 。最后希望我的文章能帮助到各位解决问题 ,以后我还会贡献更多有用的代码分享给大家。各位同学如果觉得文章还不错 ,麻烦给关注和star,小弟在这里谢过啦!
项目地址
边栏推荐
- View lazy loading in applet
- pixi.js 爆炸效果
- [v. reverse proxy and related configurations]
- ONES 冯斌:从工程师到CTO,与不确定性和信息不完备为伴|ONES Talk
- ARToolKit for Unity 入门
- BAT和FAANG的时代翻篇儿了,谁来接棒?
- I learned that automated testing is so popular after I got 2w+ in a month
- Simple example of mongoose connecting multiple databases
- III. servername matching rules
- Positioning and analysis of common delay problems in redis
猜你喜欢

Huawei announced its top ten inventions: efficient additive network and multi-objective game intelligent driving

企业数字化转型该如何做?三个融合、三个转换

一年进账34.46亿,又一个福建老乡要IPO了

自如涨价,会逼退年轻人吗?

From outsourcing to self research and then to large factories, who knows how I came here in the past five years

品牌焕新、产品上新、营销创新,东风标致的向上之路.

细说腾讯如何做到直播延时降低90%以上方案

【MUI+Flask+MongoDB+HBuilderX】APP开发之答题积分逻辑详解

混动大年,比亚迪的风评真要反转?

ECCV 2020 | star: pedestrian trajectory prediction model based on transformer (II)
随机推荐
The company's new Alibaba P6 was dismissed because the writing of test cases was not standardized, so it was sad to lie flat?
Some tool functions commonly used in JS projects
混动大年,比亚迪的风评真要反转?
JS implements JSON array merging and de duplication
数组去重
基于tensorflow的猫狗分类
[VI. load balancing and related configurations]
如何使用谷歌插件为网站注入代码
BAT和FAANG的时代翻篇儿了,谁来接棒?
Blog recommended | bookkeeper - Apache pulsar high availability / strong consistency / low latency storage implementation
Redis跳跃表的基本原理和实现
因停止俄罗斯业务:IBM Q1 损失 20 亿、思科 13 亿、Adobe 5 亿、惠普 67 亿、UiPath 1 亿
tmux(Terminal MultipleXer)命令使用
工资管理系统该如何使用?
typecho 评论回复艾特评论人
MySQL数据库用户权限管理
Taoist friends! What is the expiration policy of redis?
Why do SQL statements use indexes but still slow queries?
基于模板配置的数据可视化平台
InfoQ geek media's 15th anniversary solicitation of papers | design practice of micro service architecture in the cloud native Era