1# picker开发指导 2 3picker是滑动选择器组件,类型支持普通选择器、日期选择器、时间选择器、时间日期选择器和多列文本选择器。具体用法请参考[picker API](../reference/arkui-js/js-components-basic-picker.md)。 4 5 6## 创建picker组件 7 8在pages/index目录下的hml文件中创建一个picker组件。 9 10```html 11<!-- xxx.hml --> 12<div class="container"> 13 <picker> picker </picker> 14</div> 15``` 16 17```css 18/* xxx.css */ 19.container { 20 width: 100%; 21 height: 100%; 22 flex-direction: column; 23 justify-content: center; 24 align-items: center; 25 background-color: #F1F3F5; 26} 27``` 28 29![zh-cn_image_0000001210951541](figures/zh-cn_image_0000001210951541.gif) 30 31 32## 设置picker类型 33 34通过设置picker的type属性来选择滑动选择器类型,如定义picker为日期选择器。 35 36```html 37<!-- xxx.hml --> 38<div class="container"> 39 <picker id="picker_text" type="text" value="{{textvalue}}"range="{{rangetext}}" class="pickertext" ></picker> 40 <picker id="picker_date" type="date" value="{{datevalue}}" lunarswitch="true" start="2002-2-5" end="2030-6-5" class="pickerdate"></picker> 41</div> 42``` 43 44```css 45/* xxx.css */ 46.container { 47 width: 100%; 48 height: 100%; 49 flex-direction: column; 50 justify-content: center; 51 align-items: center; 52 background-color: #F1F3F5; 53} 54.pickertext{ 55 margin-bottom: 30px; 56} 57``` 58 59```js 60// xxx.js 61export default { 62 data: { 63 rangetext:['15', "20", "25"], 64 textvalue:'Select text', 65 datevalue:'Select date', 66 } 67} 68``` 69 70![zh-cn_image_0000001189098638](figures/zh-cn_image_0000001189098638.gif) 71 72> **说明:** 73> 74> 普通选择器设置取值范围时,需要使用数据绑定的方式。 75 76 77## 设置时间展现格式 78 79picker的hours属性定义时间的展现格式,可选类型有12小时制和24小时制。 80 81```html 82<!-- xxx.hml --> 83<div class="container"> 84 <picker id="picker_time" type="time" value="12-hour format" hours="12" onchange="timeonchange" class="pickertime"></picker> 85 <picker id="picker_time" type="time" value="24-hour format" hours="24" onchange="timeonchange" class="pickertime"></picker> 86</div> 87``` 88 89```css 90/* xxx.css */ 91.container { 92 width: 100%; 93 height: 100%; 94 flex-direction: column; 95 justify-content: center; 96 align-items: center; 97 background-color: #F1F3F5; 98} 99.pickertime { 100 margin-bottom:50px; 101 width: 300px; 102 height: 50px; 103} 104``` 105 106![zh-cn_image_0000001234327855](figures/zh-cn_image_0000001234327855.gif) 107 108> **说明:** 109> - hours属性为12:按照12小时制显示,用上午和下午进行区分; 110> 111> - hours属性为24:按照24小时制显示。 112 113 114## 添加响应事件 115 116对picker添加change和cancel事件,来对选择的内容进行确定和取消。 117 118```html 119<!-- xxx.hml --> 120<div class="container"> 121 <picker id="picker_multi" type="multi-text" value="{{multitextvalue}}" columns="3" range="{{multitext}}" selected=" 122 {{multitextselect}}" onchange="multitextonchange" oncancel="multitextoncancel" class="pickermuitl"></picker> 123</div> 124``` 125 126```css 127/* xxx.css */ 128.container { 129 width: 100%; 130 height: 100%; 131 flex-direction: column; 132 justify-content: center; 133 align-items: center; 134 background-color: #F1F3F5; 135} 136.pickermuitl { 137 margin-bottom:20px; 138 width: 600px; 139 height: 50px; 140 font-size: 25px; 141 letter-spacing:15px; 142} 143``` 144 145```js 146// xxx.js 147import promptAction from '@ohos.promptAction'; 148export default { 149 data: { 150 multitext:[["a", "b", "c"], ["e", "f", "g"], ["h", "i"]], 151 multitextvalue:'Select multi-line text', 152 multitextselect:[0,0,0], 153 }, 154 multitextonchange(e) { 155 this.multitextvalue=e.newValue; 156 promptAction.showToast({ message:"Multi-column text changed to:" + e.newValue }) 157 }, 158 multitextoncancel() { 159 promptAction.showToast({ message:"multitextoncancel" }) 160 }, 161} 162``` 163 164![zh-cn_image_0000001234009343](figures/zh-cn_image_0000001234009343.gif) 165 166 167## 场景示例 168 169 170在本场景中,开发者可以自定义填写当前的健康情况来进行打卡。 171 172 173```html 174<!-- xxx.hml --> 175<div class="doc-page"> 176 <text class="title">Health check-in</text> 177 <div class="out-container"> 178 <text class="txt">Office:</text> 179 <picker class="pick" focusable="true" type="text" value="{{pos}}" range="{{posarr}}" onchange="setPos"></picker> 180 </div> 181 <divider class="dvd"></divider> 182 <div class="out-container"> 183 <text class="txt">Office hours:</text> 184 <picker class="pick" type="date" value="{{datevalue}}" start="2002-2-5" end="2030-6-5" selected="{{dateselect}}" 185 lunarswitch="true" onchange="dateonchange"></picker> 186 </div> 187 <divider class="dvd"></divider> 188 <div class="out-container"> 189 <text class="txt">Having fever or cold symptoms</text> 190 <picker class="pick" type="text" value="{{yorn1}}" range="{{yesno}}" selected="1" onchange="isFever"></picker> 191 </div> 192 <divider class="dvd"></divider> 193 <div class="out-container"> 194 <text class="txt">Close contact with someone with COVID-19</text> 195 <picker class="pick" type="text" value="{{yorn2}}" range="{{yesno}}" selected="1" onchange="isTouch"></picker> 196 </div> 197 <div class="out-container"> 198 <button value="Submit" style="margin-top:100px;width:50%;font-color:#0000ff;height:80px" onclick="showtoast"></button> 199 </div> 200</div> 201``` 202 203 204```css 205/* xxx.css */ 206.doc-page { 207 flex-direction: column; 208 background-color: #F1F3F5; 209} 210.title { 211 margin-top: 30px; 212 margin-bottom: 30px; 213 margin-left: 50px; 214 font-weight: bold; 215 color: #0000ff; 216 font-size: 38px; 217} 218.out-container { 219 flex-direction: column; 220 align-items: center; 221} 222.pick { 223 width: 80%; 224 height: 76px; 225 border: 1px solid #0000ff; 226 border-radius: 20px; 227 padding-left: 12px; 228} 229.txt { 230 width: 80%; 231 font-size: 18px; 232 text-align: left; 233 margin-bottom: 12px; 234 margin-left: 12px; 235} 236.dvd { 237 margin-top: 30px; 238 margin-bottom: 30px; 239 margin-left: 80px; 240 margin-right: 80px; 241 color: #6495ED; 242 stroke-width: 6px; 243} 244``` 245 246 247```js 248// xxx.js 249import promptAction from '@ohos.promptAction' 250export default { 251 data: { 252 yorn1:'No', 253 yorn2:'No', 254 pos:'Home', 255 yesno:['Yes', 'No'], 256 posarr:['Home', 'Company'], 257 datevalue:'Select time', 258 datetimeselect:'2012-5-6-11-25', 259 dateselect:'2021-9-17', 260 showbuild:true 261 }, 262 onInit() { 263 }, 264 isFever(e) { 265 this.yorn1 = e.newValue 266 }, 267 isTouch(e) { 268 this.yorn2 = e.newValue 269 }, 270 setPos(e) { 271 this.pos = e.newValue 272 if (e.newValue === 'Non-research center') { 273 this.showbuild = false 274 } else { 275 this.showbuild = true 276 } 277 }, 278 setbuild(e) { 279 this.build = e.newValue 280 }, 281 dateonchange(e) { 282 e.month=e.month+1; 283 this.datevalue = e.year + "-" + e.month + "-" + e.day; 284 promptAction.showToast({ message:"date:"+e.year+"-"+e.month+"-"+e.day }) 285 }, 286 showtoast() { 287 promptAction.showToast({ 288 message: 'Submitted.', 289 duration: 2000, 290 gravity: 'center' 291 }) 292 } 293} 294``` 295 296 297![zh-cn_image_0000001234342189](figures/zh-cn_image_0000001234342189.gif) 298 299 300## 相关实例 301 302针对picker开发,有以下相关实例可供参考: 303 304- [`JsComponentCollection`:JS组件集合(JS)(API9)](https://gitee.com/openharmony/applications_app_samples/tree/OpenHarmony-3.2-Release/code/UI/JsComponentClollection/JsComponentCollection)