picker开发指导
==========

picker是滑动选择器组件，类型支持普通选择器、日期选择器、时间选择器、时间日期选择器和多列文本选择器。具体用法请参考[picker](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-components-basic-picker)。

#### 创建picker组件

在pages/index目录下的hml文件中创建一个picker组件。

<!-- xxx.hml -->
<div class="container">
  <picker>picker</picker>
</div>

/\* xxx.css \*/
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/45/v3/nW0BLAWEQMGphRhl8GIdGQ/zh-cn_image_0000002664327943.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132321Z&HW-CC-Expire=86400&HW-CC-Sign=2F70986098E8D35C040F55D2D941165EE014E5AC81DFC6C3436763CCC2E9744A)

#### 设置picker类型

通过设置picker的type属性来选择滑动选择器类型，如定义picker为日期选择器。

<!-- xxx.hml -->
<div class="container">
  <picker id="picker\_text" type="text" value="{{textvalue}}"range="{{rangetext}}" class="pickertext" ></picker>
  <picker id="picker\_date" type="date" value="{{datevalue}}" lunarswitch="true" start="2002-2-5" end="2030-6-5" class="pickerdate"></picker>
</div>

/\* xxx.css \*/
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
.pickertext{
  margin-bottom: 30px;
}

// xxx.js
export default {
  data: {
    rangetext:\['15', "20", "25"\],
    textvalue:'Select text',
    datevalue:'Select date',
  }
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/e1/v3/Z42dLA82SGqr_tLBVL1CCA/zh-cn_image_0000002633848834.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132321Z&HW-CC-Expire=86400&HW-CC-Sign=4CA5A84E9AEAD2F09E4C159A668008952E64843193B09393D057D0777714839D)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/cd/v3/USrILH_yRY-s28OBsVV0xw/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132321Z&HW-CC-Expire=86400&HW-CC-Sign=B02C2F8F55DD0A8C8E7129890C354EE80B4F2B8385890243903D2680340627B1)

普通选择器设置取值范围时，需要使用数据绑定的方式。

#### 设置时间显示格式

picker组件的hours属性用于设置时间显示格式，支持12小时制和24小时制两种模式。

<!-- xxx.hml -->
<div class="container">
  <picker id="picker\_time" type="time" value="12-hour format" hours="12" onchange="timeonchange"  class="pickertime"></picker>
  <picker id="picker\_time" type="time" value="24-hour format" hours="24" onchange="timeonchange"  class="pickertime"></picker>
</div>

/\* xxx.css \*/
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
.pickertime {
  margin-bottom:50px;
  width: 300px;
  height: 50px;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/bc/v3/_fHOBFTJTQyhL1G-i1m3Xg/zh-cn_image_0000002634008738.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132321Z&HW-CC-Expire=86400&HW-CC-Sign=1CA5A196BF5F95ADC2643DD5D0222B2444C07AED54BC2D7B592B90D7E8D0DB87)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/83/v3/L8lrtvV3RFSmaX2VOWj0Lg/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132321Z&HW-CC-Expire=86400&HW-CC-Sign=462B6EA244D072717E845D512434BC628BA8CEB5D3243E76CAD155D8064979FA)

*   hours属性为12：按照12小时制显示，用上午和下午进行区分。
    
*   hours属性为24：按照24小时制显示。
    

#### 添加响应事件

为picker组件添加change和cancel事件，可以处理用户的选择确定和取消操作。

<!-- xxx.hml -->
<div class="container">
  <picker id="picker\_multi" type="multi-text" value="{{multitextvalue}}" columns="3" range="{{multitext}}" selected="
     {{multitextselect}}" onchange="multitextonchange" oncancel="multitextoncancel" class="pickermuitl"></picker>
</div>

/\* xxx.css \*/
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
.pickermuitl {
  margin-bottom:20px;
  width: 600px;
  height: 50px;
  font-size: 25px;
  letter-spacing:15px;
}

// xxx.js
import promptAction from '@ohos.promptAction';
export default {
  data: {
    multitext:\[\["a", "b", "c"\], \["e", "f", "g"\], \["h", "i"\]\],
    multitextvalue:'Select multi-line text',
    multitextselect:\[0,0,0\],
  },
  multitextonchange(e) {
    this.multitextvalue=e.newValue;
    promptAction.showToast({ message:"Multi-column text changed to:" + e.newValue })
  },
  multitextoncancel() {
    promptAction.showToast({ message:"multitextoncancel" })
  },
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/e/v3/WWUPcKeDTIu2VXV1is3PkA/zh-cn_image_0000002664207885.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132321Z&HW-CC-Expire=86400&HW-CC-Sign=B9921378CEDF48252BF13EF460F7D2E61BFDB3201BB4556C8BC6F54FBC66EE6E)

#### 场景示例

在本场景中，开发者可以自定义填写健康情况以完成打卡。

<!-- xxx.hml -->
<div class="doc-page">
  <text class="title">Health check-in</text>
  <div class="out-container">
    <text class="txt">Office:</text>
    <picker class="pick" focusable="true" type="text" value="{{pos}}" range="{{posarr}}" onchange="setPos"></picker>
  </div>
  <divider class="dvd"></divider>
  <div class="out-container">
    <text class="txt">Office hours:</text>
    <picker class="pick" type="date" value="{{datevalue}}"  start="2002-2-5" end="2030-6-5" selected="{{dateselect}}"
      lunarswitch="true" onchange="dateonchange"></picker>
  </div>
  <divider class="dvd"></divider>
  <div class="out-container">
    <text class="txt">Having fever or cold symptoms</text>
    <picker class="pick" type="text" value="{{yorn1}}" range="{{yesno}}" selected="1" onchange="isFever"></picker>
  </div>
  <divider class="dvd"></divider>
  <div class="out-container">
    <text class="txt">Close contact with someone with COVID-19</text>
    <picker class="pick" type="text" value="{{yorn2}}" range="{{yesno}}" selected="1" onchange="isTouch"></picker>
  </div>
  <div class="out-container">
    <button value="Submit" style="margin-top:100px;width:50%;font-color:#0000ff;height:80px" onclick="showtoast"></button>
  </div>
</div>

/\* xxx.css \*/
.doc-page {
  flex-direction: column;
  background-color: #F1F3F5;
}
.title {
  margin-top: 30px;
  margin-bottom: 30px;
  margin-left: 50px;
  font-weight: bold;
  color: #0000ff;
  font-size: 38px;
}
.out-container {
  flex-direction: column;
  align-items: center;
}
.pick {
  width: 80%;
  height: 76px;
  border: 1px solid #0000ff;
  border-radius: 20px;
  padding-left: 12px;
}
.txt {
  width: 80%;
  font-size: 18px;
  text-align: left;
  margin-bottom: 12px;
  margin-left: 12px;
}
.dvd {
  margin-top: 30px;
  margin-bottom: 30px;
  margin-left: 80px;
  margin-right: 80px;
  color: #6495ED;
  stroke-width: 6px;
}

// xxx.js
import promptAction from '@ohos.promptAction'
export default {
  data: {
    yorn1:'No',
    yorn2:'No',
    pos:'Home',
    yesno:\['Yes', 'No'\],
    posarr:\['Home', 'Company'\],
    datevalue:'Select time',
    datetimeselect:'2012-5-6-11-25',
    dateselect:'2021-9-17',
    showbuild:true
  },
  onInit() {
  },
  isFever(e) {
    this.yorn1 = e.newValue
  },
  isTouch(e) {
    this.yorn2 = e.newValue
  },
  setPos(e) {
    this.pos = e.newValue
    if (e.newValue === 'Non-research center') {
      this.showbuild = false
    } else {
      this.showbuild = true
    }
  },
  setbuild(e) {
    this.build = e.newValue
  },
  dateonchange(e) {
    e.month=e.month+1;
    this.datevalue = e.year + "-" + e.month + "-" + e.day;
    promptAction.showToast({ message:"date:"+e.year+"-"+e.month+"-"+e.day })
  },
  showtoast() {
    promptAction.showToast({
      message: 'Submitted.',
      duration: 2000,
      gravity: 'center'
    })
  }
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/f9/v3/PeIIBz8JTBK6rCK2fzTQIA/zh-cn_image_0000002664327945.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132321Z&HW-CC-Expire=86400&HW-CC-Sign=0AB2B4BEB0C58BD0CDE41416BADBCF8DC5B1115B1030120AD000DABD58D726AB)