1e41f4b71Sopenharmony_ci# input 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci交互式组件,包括单选框,多选框,按钮。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 该组件从从API version 4 开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## 子组件 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci不支持。 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## 属性 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci| 名称 | 类型 | 默认值 | 必填 | 描述 | 18e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 19e41f4b71Sopenharmony_ci| type | string | <br/>button | 否 | input组件类型,可选值为button,checkbox,radio。<br/><br/>button,checkbox,radio不支持动态修改。可选值定义如下:<br/>- button:定义可点击的按钮;<br/>- checkbox:定义多选框;<br/>- radio:定义单选按钮,允许在多个拥有相同name值的选项中选中其中一个; | 20e41f4b71Sopenharmony_ci| checked | boolean | false | 否 | 当前组件是否选中,仅type为checkbox和radio生效。 | 21e41f4b71Sopenharmony_ci| name | string | - | 否 | input组件的名称。 | 22e41f4b71Sopenharmony_ci| value | string | - | 否 | input组件的value值,当类型为radio时必填且相同name值的选项该值唯一。 | 23e41f4b71Sopenharmony_ci| id | string | - | 否 | 组件的唯一标识。 | 24e41f4b71Sopenharmony_ci| style | string | - | 否 | 组件的样式声明。 | 25e41f4b71Sopenharmony_ci| class | string | - | 否 | 组件的样式类,用于引用样式表。 | 26e41f4b71Sopenharmony_ci| ref | string | - | 否 | 用来指定指向子元素的引用信息,该引用将注册到父组件的$refs 属性对象上。 | 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci## 事件 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci- 当input类型为checkbox、radio时,支持如下事件: 32e41f4b71Sopenharmony_ci | 名称 | 参数 | 描述 | 33e41f4b71Sopenharmony_ci | -------- | -------- | -------- | 34e41f4b71Sopenharmony_ci | change | { checked:true \| false } | checkbox多选框或radio单选框的checked状态发生变化时触发该事件。 | 35e41f4b71Sopenharmony_ci | click | - | 点击动作触发该事件。 | 36e41f4b71Sopenharmony_ci | longpress | - | 长按动作触发该事件。 | 37e41f4b71Sopenharmony_ci | swipe<sup>5+</sup> | [SwipeEvent](js-lite-common-events.md) | 组件上快速滑动后触发。 | 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci- 当input类型为button时,支持如下事件: 40e41f4b71Sopenharmony_ci | 名称 | 参数 | 描述 | 41e41f4b71Sopenharmony_ci | -------- | -------- | -------- | 42e41f4b71Sopenharmony_ci | click | - | 点击动作触发该事件。 | 43e41f4b71Sopenharmony_ci | longpress | - | 长按动作触发该事件。 | 44e41f4b71Sopenharmony_ci | swipe<sup>5+</sup> | [SwipeEvent](js-lite-common-events.md) | 组件上快速滑动后触发。 | 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci## 样式 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci| 名称 | 类型 | 默认值 | 必填 | 描述 | 50e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 51e41f4b71Sopenharmony_ci| color | <color> | \#ffffff | 否 | 按钮的文本颜色。 | 52e41f4b71Sopenharmony_ci| font-size | <length> | 30px | 否 | 按钮的文本尺寸。 | 53e41f4b71Sopenharmony_ci| width | <length> | - | 否 | type为button时,默认值为100px。 | 54e41f4b71Sopenharmony_ci| height | <length> | - | 否 | type为button时,默认值为50px。 | 55e41f4b71Sopenharmony_ci| font-family | string | SourceHanSansSC-Regular | 否 | 字体。目前仅支持SourceHanSansSC-Regular 字体。 | 56e41f4b71Sopenharmony_ci| padding | <length> | 0 | 否 | 使用简写属性设置所有的内边距属性。<br/> 该属性可以有1到4个值:<br/>- 指定一个值时,该值指定四个边的内边距。<br/>- 指定两个值时,第一个值指定上下两边的内边距,第二个指定左右两边的内边距。<br/>- 指定三个值时,第一个指定上边的内边距,第二个指定左右两边的内边距,第三个指定下边的内边距。<br/>- 指定四个值时分别为上、右、下、左边的内边距(顺时针顺序)。 | 57e41f4b71Sopenharmony_ci| padding-[left\|top\|right\|bottom] | <length> | 0 | 否 | 设置左、上、右、下内边距属性。 | 58e41f4b71Sopenharmony_ci| margin | <length> \| <percentage><sup>5+</sup> | 0 | 否 | 使用简写属性设置所有的外边距属性,该属性可以有1到4个值。<br/>- 只有一个值时,这个值会被指定给全部的四个边。<br/>- 两个值时,第一个值被匹配给上和下,第二个值被匹配给左和右。<br/>- 三个值时,第一个值被匹配给上, 第二个值被匹配给左和右,第三个值被匹配给下。<br/>- 四个值时,会依次按上、右、下、左的顺序匹配 (即顺时针顺序)。 | 59e41f4b71Sopenharmony_ci| margin-[left\|top\|right\|bottom] | <length> \| <percentage><sup>5+</sup> | 0 | 否 | 设置左、上、右、下外边距属性。 | 60e41f4b71Sopenharmony_ci| border-width | <length> | 0 | 否 | 使用简写属性设置元素的所有边框宽度。 | 61e41f4b71Sopenharmony_ci| border-color | <color> | black | 否 | 使用简写属性设置元素的所有边框颜色。 | 62e41f4b71Sopenharmony_ci| border-radius | <length> | - | 否 | border-radius属性是设置元素的外边框圆角半径。 | 63e41f4b71Sopenharmony_ci| background-color | <color> | - | 否 | 设置背景颜色。 | 64e41f4b71Sopenharmony_ci| display | string | flex | 否 | 确定一个元素所产生的框的类型,可选值为:<br/>- flex:弹性布局。<br/>- none:不渲染此元素。 | 65e41f4b71Sopenharmony_ci| [left\|top] | <length> \| <percentage><sup>6+</sup> | - | 否 | left\|top确定元素的偏移位置。<br/>- left属性规定元素的左边缘。该属性定义了定位元素左外边距边界与其包含块左边界之间的偏移。<br/>- top属性规定元素的顶部边缘。该属性定义了一个定位元素的上外边距边界与其包含块上边界之间的偏移。 | 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci## 示例 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci1. type为button 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci ```html 72e41f4b71Sopenharmony_ci <!-- xxx.hml --> 73e41f4b71Sopenharmony_ci <div class="div-button"> 74e41f4b71Sopenharmony_ci <input class="button" type="button" value="Input-Button"></input> 75e41f4b71Sopenharmony_ci </div> 76e41f4b71Sopenharmony_ci ``` 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci ```css 79e41f4b71Sopenharmony_ci /* xxx.css */ 80e41f4b71Sopenharmony_ci .div-button { 81e41f4b71Sopenharmony_ci flex-direction: column; 82e41f4b71Sopenharmony_ci align-items: center; 83e41f4b71Sopenharmony_ci width: 100%; 84e41f4b71Sopenharmony_ci height: 100%; 85e41f4b71Sopenharmony_ci } 86e41f4b71Sopenharmony_ci .button { 87e41f4b71Sopenharmony_ci margin-top: 30px; 88e41f4b71Sopenharmony_ci width: 280px; 89e41f4b71Sopenharmony_ci } 90e41f4b71Sopenharmony_ci ``` 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci  93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci2. type为checkbox 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci ```html 99e41f4b71Sopenharmony_ci <!-- xxx.hml --> 100e41f4b71Sopenharmony_ci <div class="content"> 101e41f4b71Sopenharmony_ci <input onchange="checkboxOnChange" checked="true" type="checkbox"></input> 102e41f4b71Sopenharmony_ci <text class="text">{{text}}</text> 103e41f4b71Sopenharmony_ci </div> 104e41f4b71Sopenharmony_ci ``` 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci ```css 107e41f4b71Sopenharmony_ci /* xxx.css */ 108e41f4b71Sopenharmony_ci .content{ 109e41f4b71Sopenharmony_ci width: 100%; 110e41f4b71Sopenharmony_ci height: 100%; 111e41f4b71Sopenharmony_ci flex-direction: column; 112e41f4b71Sopenharmony_ci align-items: center; 113e41f4b71Sopenharmony_ci justify-content: center; 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci .text{ 116e41f4b71Sopenharmony_ci font-size: 30px; 117e41f4b71Sopenharmony_ci text-align: center; 118e41f4b71Sopenharmony_ci width: 200px; 119e41f4b71Sopenharmony_ci margin-top: 20px; 120e41f4b71Sopenharmony_ci height: 100px; 121e41f4b71Sopenharmony_ci } 122e41f4b71Sopenharmony_ci ``` 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci ```javascript 125e41f4b71Sopenharmony_ci // xxx.js 126e41f4b71Sopenharmony_ci export default { 127e41f4b71Sopenharmony_ci data: { 128e41f4b71Sopenharmony_ci text: "text" 129e41f4b71Sopenharmony_ci }, 130e41f4b71Sopenharmony_ci checkboxOnChange(e) { 131e41f4b71Sopenharmony_ci this.text = e.checked; 132e41f4b71Sopenharmony_ci } 133e41f4b71Sopenharmony_ci } 134e41f4b71Sopenharmony_ci ``` 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci  137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci3. type为radio 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci ```html 141e41f4b71Sopenharmony_ci <!-- xxx.hml --> 142e41f4b71Sopenharmony_ci <div class="container"> 143e41f4b71Sopenharmony_ci <div class="item"> 144e41f4b71Sopenharmony_ci <input type="radio" checked="true" name="radioSample" value="radio1" onchange="onRadioChange"></input> 145e41f4b71Sopenharmony_ci <text class="text">radio1</text> 146e41f4b71Sopenharmony_ci </div> 147e41f4b71Sopenharmony_ci <div class="item"> 148e41f4b71Sopenharmony_ci <input type="radio" checked="false" name="radioSample" value="radio2" onchange="onRadioChange"></input> 149e41f4b71Sopenharmony_ci <text class="text">radio2</text> 150e41f4b71Sopenharmony_ci </div> 151e41f4b71Sopenharmony_ci <div class="item"> 152e41f4b71Sopenharmony_ci <input type="radio" checked="false" name="radioSample" value="radio3" onchange="onRadioChange"></input> 153e41f4b71Sopenharmony_ci <text class="text">radio3</text> 154e41f4b71Sopenharmony_ci </div> 155e41f4b71Sopenharmony_ci </div> 156e41f4b71Sopenharmony_ci ``` 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci ```css 159e41f4b71Sopenharmony_ci /* xxx.css */ 160e41f4b71Sopenharmony_ci .container { 161e41f4b71Sopenharmony_ci width: 100%; 162e41f4b71Sopenharmony_ci height: 100%; 163e41f4b71Sopenharmony_ci justify-content: center; 164e41f4b71Sopenharmony_ci align-items: center; 165e41f4b71Sopenharmony_ci flex-direction: column; 166e41f4b71Sopenharmony_ci } 167e41f4b71Sopenharmony_ci .item { 168e41f4b71Sopenharmony_ci width: 50%; 169e41f4b71Sopenharmony_ci height: 30%; 170e41f4b71Sopenharmony_ci justify-content: center; 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci .text { 173e41f4b71Sopenharmony_ci margin-top: 25%; 174e41f4b71Sopenharmony_ci font-size: 30px; 175e41f4b71Sopenharmony_ci text-align: center; 176e41f4b71Sopenharmony_ci width: 200px; 177e41f4b71Sopenharmony_ci height: 100px; 178e41f4b71Sopenharmony_ci } 179e41f4b71Sopenharmony_ci ``` 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci 