input
输入框。该组件是原生组件,使用时请注意相关限制。
属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|---|
native | Boolean | true | 否 | 为 true 时为原生组件,false时使用参考Html input元素, 不能动态变更 | |
value | String | 否 | 输入框的初始内容 | ||
type | String | text | 否 | input 的类型 | |
password | Boolean | false | 否 | 是否是密码类型 | |
placeholder | String | 否 | 输入框为空时占位符 | ||
placeholder-style | String | 否 | 指定 placeholder 的样式 | ||
placeholder-class | String | input-placeholder | 否 | 指定 placeholder 的样式类 | |
disabled | Boolean | false | 否 | 是否禁用 | |
maxlength | Number | 140 | 否 | 最大输入长度,设置为 -1 的时候不限制最大长度 | |
cursor-spacing | Number | 0 | 否 | 指定光标与键盘的距离,取 input 距离底部的距离和 cursor-spacing 指定的距离的最小值作为光标与键盘的距离 | |
auto-focus | Boolean | false | 否 | (即将废弃,请直接使用 focus )自动聚焦,拉起键盘 | |
focus | Boolean | false | 否 | 获取焦点 | |
confirm-type | String | done | 否 | 设置键盘右下角按钮的文字,仅在type='text'时生效 | |
confirm-hold | Boolean | false | 否 | 点击键盘右下角按钮时是否保持键盘不收起 | |
cursor | Number | 否 | 指定focus时的光标位置 | ||
selection-start | Number | -1 | 否 | 光标起始位置,自动聚集时有效,需与selection-end搭配使用 | |
selection-end | Number | -1 | 否 | 光标结束位置,自动聚集时有效,需与selection-start搭配使用 | |
adjust-position | Boolean | true | 否 | 键盘弹起时,是否自动上推页面 | |
hold-keyboard | Boolean | false | 否 | focus时,点击页面的时候不收起键盘 | |
bindinput | EventHandle | 否 | 键盘输入时触发,event.detail = {value, cursor, keyCode},keyCode 为键值。 | ||
bindfocus | EventHandle | 否 | 输入框聚焦时触发,event.detail = { value, height },height 为键盘高度 | ||
bindblur | EventHandle | 否 | 输入框失去焦点时触发,event.detail = { value } | ||
bindconfirm | EventHandle | 否 | 点击完成按钮时触发,event.detail = { value } | ||
bindkeyboardheightchange | EventHandle | 否 | 键盘高度发生变化的时候触发此事件,event.detail = {height: height, duration: duration} |
type 的合法值
值 | 说明 | 最低版本 |
---|---|---|
text | 文本输入键盘 | |
Number | 数字输入键盘 | |
idcard | 身份证输入键盘 | |
digit | 带小数点的数字键盘 | |
phone | 电话输入键盘 | |
url | 网址输入键盘 | |
邮箱输入键盘 |
confirm-type有效值
值 | 说明 | 最低版本 |
---|---|---|
send | 右下角按钮为“发送” | |
search | 右下角按钮为“搜索” | |
next | 右下角按钮为“下一个” | |
go | 右下角按钮为“前往” | |
done | 右下角按钮为“完成” |
Tips:
- native 为false时,部分键盘增强属性和事件将失效
- native 为ture时,input 是一个原生组件,字体是系统字体,所以无法设置 font-family
- 不建议输入时对用户输入进行修改
- confirm-type的最终表现与手机输入法本身的实现有关,部分安卓系统输入法和第三方输入法可能不支持或不完全支持
示例代码:
<!--pages/component/pages/input/input.mcml-->
<view class="container">
<view class="statement">输入框。</view>
<view class="cssname">type</view>
<view mc:for="{{types}}" class="inputClass">
<input style="height:100%;" type="{{item.type}}" placeholder="{{item.desc}}"></input>
</view>
<view class="cssname">confirm-type</view>
<view mc:for="{{confrimTypes}}" class="inputClass">
<input confirm-type="{{item.type}}" style="height:100%;" password="{{item.password || false}}" type="text" placeholder="{{item.desc}}"></input>
</view>
<view class="cssname">password: true, placeholder-style: placeholder 的样式</view>
<view class="inputClass">
<input style="height:100%;" password="true" placeholder="密码输入框" placeholder-style="color:red;"></input>
</view>
<view class="cssname">disabled: true</view>
<view class="inputClass">
<input style="height:100%;" disabled="true" value="禁用的输入框"></input>
</view>
<view class="cssname">maxlength: 3, placeholder-class:placeholder 的样式类</view>
<view class="inputClass">
<input style="height:100%;" maxlength="3" placeholder-class="placeholderClass" placeholder="最大长度3个字符"></input>
</view>
<view class="cssname">focus: true, cursor : 3</view>
<view class="inputClass">
<input style="height:100%;" auto-focus="true" cursor="3" focus="true" value="自动焦点" placeholder=""></input>
</view>
<view class="cssname">confirm-hold: true</view>
<view class="inputClass">
<input style="height:100%;" confirm-hold="true" placeholder="点击键盘右下角按钮时保持键盘不收起"></input>
</view>
<view class="cssname">adjust-position: false</view>
<view class="inputClass">
<input style="height:100%;" adjust-position="{{false}}" placeholder="键盘弹起时,不上推页面"></input>
</view>
<view class="cssname">hold-keyboard: true</view>
<view class="inputClass">
<input style="height:100%;" hold-keyboard="{{true}}" placeholder="focus时,点击页面的时候不收起键盘"></input>
</view>
<view class="cssname">cursor-spacing : 30</view>
<view class="inputClass">
<input cursor-spacing="30" value="{{selectionText}}"
focus="{{isSelection}}"
selection-start="2"
selection-end="7"></input>
</view>
<view class="cssname">selection-start : 2, selection-end: 7</view>
<view>
<button bindtap="selectionTap">自动焦点时,设置光标选中</button>
</view>
<view class="cssname">keyboard-accessory</view>
<view class="textareaClass">
<input placeholder="设置键盘上方工具栏视图">
<keyboard-accessory style="display:flex;height: 50px;">
<cover-view bindtap="keyboardTap" style="flex: 1; background: green;">1</cover-view>
<cover-view bindtap="keyboardTap" style="flex: 1; background: red;">2</cover-view>
</keyboard-accessory>
</input>
</view>
<view class="cssname">事件绑定</view>
<view class="inputClass">
<input
bindfocus="bindfocus"
bindinput="bindinput"
bindblur="bindblur"
bindconfirm="bindconfirm"
bindkeyboardheightchange="bindkeyboardheightchange"
></input>
</view>
<view class="cssname">bindfocus:输入框聚焦时触发, 参数event.detail</view>
<text space="nbsp" user-select="true" class="scrollDetail">{{bindfocus}}</text>
<view class="cssname">bindinput:键盘输入时触发, 参数event.detail</view>
<text space="nbsp" user-select="true" class="scrollDetail">{{bindinput}}</text>
<view class="cssname">bindblur:输入框失去焦点时触发, 参数event.detail</view>
<text space="nbsp" user-select="true" class="scrollDetail">{{bindblur}}</text>
<view class="cssname">bindconfirm:点击完成按钮时触发, 参数event.detail</view>
<text space="nbsp" user-select="true" class="scrollDetail">{{bindconfirm}}</text>
<view class="cssname">bindkeyboardheightchange:键盘高度发生变化的时候触发, 参数event.detail</view>
<text space="nbsp" user-select="true" class="scrollDetail">{{bindkeyboardheightchange}}</text>
</view>
// pages/component/pages/input/input.js
Page({
data: {
types:[
{ type: 'text', desc: 'text:默认文本输入键盘' },
{ type: 'number', desc: 'number:纯数字输入键盘' },
{ type: 'digit', desc: 'digit:带小数点的数字键盘' },
{ type: 'idcard', desc: 'idcard:身份证输入键盘' },
{ type: 'phone', desc: 'phone:电话输入键盘' },
{ type: 'url', desc: 'url:网址输入键盘' },
{ type: 'email', desc: 'email:邮箱输入键盘' }
],
confrimTypes: [
{ type: 'send', desc: 'send:发送' },
{ type: 'search', desc: 'search:搜索' },
{ type: 'next', desc: 'next:下一个' },
{ type: 'go', desc: 'go:前往' },
{ type: 'done', desc: 'done:完成' }
],
isSelection: false,
selectionText: '指定光标与键盘的距离'
},
bindfocus: function (e) {
this.setData({
bindfocus: JSON.stringify(e, null, 2)
})
},
bindinput: function (e) {
this.setData({
bindinput: JSON.stringify(e, null, 2)
})
},
bindblur: function (e) {
this.setData({
bindblur: JSON.stringify(e, null, 2)
})
},
bindconfirm: function (e) {
this.setData({
bindconfirm: JSON.stringify(e, null, 2)
})
},
bindkeyboardheightchange: function(e) {
this.setData({
bindkeyboardheightchange: JSON.stringify(e, null, 2)
})
},
selectionTap: function(e){
this.setData({
isSelection: true,
selectionText: '指定光标与键盘的距离'
})
},
keyboardTap: function(e){
mc.showModal({
showCancel: false,
content: '点了点我'
})
}
})