1e41f4b71Sopenharmony_ci# input
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci>  **说明:**
4e41f4b71Sopenharmony_ci>  从API version 4开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci交互式组件,包括单选框,多选框,按钮和单行文本输入框。
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci## 权限列表
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci## 子组件
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci不支持。
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci## 属性
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci除支持[通用属性](js-components-common-attributes.md)外,还支持如下属性:
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci| 名称                               | 类型                      | 默认值       | 必填   | 描述                                       |
23e41f4b71Sopenharmony_ci| -------------------------------- | ----------------------- | --------- | ---- | ---------------------------------------- |
24e41f4b71Sopenharmony_ci| type                             | string                  | text<br/> | 否    | input组件类型,可选值为text,email,date,time,number,password,button,checkbox,radio。<br/>其中text,email,date,time,number,password这六种类型之间支持动态切换修改。<br/>button,checkbox,radio不支持动态修改。可选值定义如下:<br/>-&nbsp;button:定义可点击的按钮;<br/>-&nbsp;checkbox:定义多选框;<br/>-&nbsp;radio:定义单选按钮,允许在多个拥有相同name值的选项中选中其中一个;<br/>-&nbsp;text:定义一个单行的文本字段;<br/>-&nbsp;email:定义用于e-mail地址的字段;<br/>-&nbsp;date:定义&nbsp;date&nbsp;控件(包括年、月、日,不包括时间);<br/>-&nbsp;time:定义用于输入时间的控件(不带时区);<br/>-&nbsp;number:定义用于输入数字的字段;<br/>-&nbsp;password:定义密码字段(字段中的字符会被遮蔽)。 |
25e41f4b71Sopenharmony_ci| checked                          | boolean                 | false     | 否    | 当前组件是否选中,仅type为checkbox和radio生效。         |
26e41f4b71Sopenharmony_ci| name                             | string                  | -         | 否    | input组件的名称。<br>type为radio时,name为必填。      |
27e41f4b71Sopenharmony_ci| value                            | string                  | -         | 否    | input组件的value值,当类型为radio时必填且相同name值的选项该值唯一。 |
28e41f4b71Sopenharmony_ci| placeholder                      | string                  | -         | 否    | 设置提示文本的内容,仅在type为text\|email\|date\|time\|number\|password时生效。 |
29e41f4b71Sopenharmony_ci| maxlength                        | number                  | -         | 否    | 输入框可输入的最多字符数量,不填表示不限制输入框中字符数量。           |
30e41f4b71Sopenharmony_ci| enterkeytype                     | string                  | default   | 否    | 不支持动态修改。<br/>设置软键盘Enter按钮的类型,可选值为:<br/>-&nbsp;default:默认<br/>-&nbsp;next:下一项<br/>-&nbsp;go:前往<br/>-&nbsp;done:完成<br/>-&nbsp;send:发送<br/>-&nbsp;search:搜索<br/>除“next”外,点击后会自动收起软键盘。 |
31e41f4b71Sopenharmony_ci| headericon                       | string                  | -         | 否    | 在文本输入前的图标资源路径,该图标不支持点击事件(button,checkbox和radio不生效),图标格式为jpg,png和svg。 |
32e41f4b71Sopenharmony_ci| showcounter<sup>5+</sup>         | boolean                 | false     | 否    | 文本输入框是否显示计数下标,需要配合maxlength一起使用。         |
33e41f4b71Sopenharmony_ci| menuoptions<sup>5+</sup>         | Array&lt;MenuOption&gt; | -         | 否    | 设置文本选择弹框点击更多按钮之后显示的菜单项。                  |
34e41f4b71Sopenharmony_ci| autofocus<sup>6+</sup>           | boolean                 | false     | 否    | 是否自动获焦。<br/>应用首页中设置不生效,可在onActive中延迟(100-500ms左右)调用focus方法实现输入框在首页中自动获焦。 |
35e41f4b71Sopenharmony_ci| selectedstart<sup>6+</sup>       | number                  | -1        | 否    | 开始选择文本时初始选择位置。                           |
36e41f4b71Sopenharmony_ci| selectedend<sup>6+</sup>         | number                  | -1        | 否    | 开始选择文本时结尾选择位置。                           |
37e41f4b71Sopenharmony_ci| softkeyboardenabled<sup>6+</sup> | boolean                 | true      | 否    | 编辑时是否弹出系统软键盘。                            |
38e41f4b71Sopenharmony_ci| showpasswordicon<sup>6+</sup>    | boolean                 | true      | 否    | 是否显示密码框末尾的图标(仅type为password时生效)。         |
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci**表1** MenuOption<sup>5+</sup>
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci| 名称      | 类型     | 描述          |
43e41f4b71Sopenharmony_ci| ------- | ------ | ----------- |
44e41f4b71Sopenharmony_ci| icon    | string | 菜单选项中的图标路径。 |
45e41f4b71Sopenharmony_ci| content | string | 菜单选项中的文本内容。 |
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci## 样式
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci除支持[通用样式](js-components-common-styles.md)外,还支持如下样式:
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci| 名称                       | 类型                         | 默认值        | 必填   | 描述                                       |
53e41f4b71Sopenharmony_ci| ------------------------ | -------------------------- | ---------- | ---- | ---------------------------------------- |
54e41f4b71Sopenharmony_ci| color                    | &lt;color&gt;              | \#e6000000 | 否    | 单行输入框或者按钮的文本颜色。                          |
55e41f4b71Sopenharmony_ci| font-size                | &lt;length&gt;             | 16px       | 否    | 单行输入框或者按钮的文本尺寸。                          |
56e41f4b71Sopenharmony_ci| allow-scale              | boolean                    | true       | 否    | 单行输入框或者按钮的文本尺寸是否跟随系统设置字体缩放尺寸进行放大缩小。<br/>如果在config描述文件中针对ability配置了fontSize的config-changes标签,则应用不会重启而直接生效。 |
57e41f4b71Sopenharmony_ci| placeholder-color        | &lt;color&gt;              | \#99000000 | 否    | 单行输入框的提示文本的颜色,type为text\|email\|date\|time\|number\|password时生效。 |
58e41f4b71Sopenharmony_ci| font-weight              | number&nbsp;\|&nbsp;string | normal     | 否    | 单行输入框或者按钮的字体粗细,见[text组件font-weight的样式属性](js-components-basic-text.md#样式)。 |
59e41f4b71Sopenharmony_ci| caret-color<sup>6+</sup> | &lt;color&gt;              | -          | 否    | 设置输入光标的颜色。                               |
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci## 事件
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci除支持[通用事件](js-components-common-events.md)外,还支持如下事件:
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci- 当input类型为text、email、date、time、number、password时,支持如下事件:
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci  | 名称                        | 参数                                       | 描述                                       |
69e41f4b71Sopenharmony_ci  | ------------------------- | ---------------------------------------- | ---------------------------------------- |
70e41f4b71Sopenharmony_ci  | change                    | {<br>&nbsp;value: inputValue<br>&nbsp;}  | 输入框输入内容发生变化时触发该事件,返回用户当前输入值。<br/>改变value属性值不会触发该回调。 |
71e41f4b71Sopenharmony_ci  | enterkeyclick             | {<br>&nbsp;value: enterKey<br>&nbsp;}    | 软键盘enter键点击后触发该事件,返回enter按钮的类型,enterKey类型为number,可选值为:<br/>-&nbsp;2:设置enterkeytype属性为go时生效。<br/>-&nbsp;3:设置enterkeytype属性为search时生效。<br/>-&nbsp;4:设置enterkeytype属性为send时生效。<br/>-&nbsp;5:设置enterkeytype属性为next时生效。<br/>-&nbsp;6:不设置enterkeytype或者设置enterkeytype属性为default、done时生效。 |
72e41f4b71Sopenharmony_ci  | translate<sup>5+</sup>    | {<br>&nbsp;value:&nbsp;selectedText<br>&nbsp;} | 设置此事件后,进行文本选择操作后文本选择弹窗会出现翻译按钮,点击翻译按钮之后,触发该回调,返回选中的文本内容。 |
73e41f4b71Sopenharmony_ci  | share<sup>5+</sup>        | {<br>&nbsp;value:&nbsp;selectedText<br>&nbsp;} | 设置此事件后,进行文本选择操作后文本选择弹窗会出现分享按钮,点击分享按钮之后,触发该回调,返回选中的文本内容。 |
74e41f4b71Sopenharmony_ci  | search<sup>5+</sup>       | {<br>&nbsp;value:&nbsp;selectedText<br>&nbsp;} | 设置此事件后,进行文本选择操作后文本选择弹窗会出现搜索按钮,点击搜索按钮之后,触发该回调,返回选中的文本内容。 |
75e41f4b71Sopenharmony_ci  | optionselect<sup>5+</sup> | {<br>&nbsp;index: optionIndex,<br>&nbsp;value:&nbsp;selectedText<br>&nbsp;} | 文本选择弹窗中设置menuoptions属性后,用户在文本选择操作后,点击菜单项后触发该回调,返回点击的菜单项序号和选中的文本内容。 |
76e41f4b71Sopenharmony_ci  | selectchange<sup>6+</sup> | {<br>start:&nbsp;number,<br>end:&nbsp;number<br>&nbsp;} | 文本选择变化时触发事件。                             |
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci- 当input类型为checkbox、radio时,支持如下事件:
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci  | 名称     | 参数                                       | 描述                                       |
81e41f4b71Sopenharmony_ci  | ------ | ---------------------------------------- | ---------------------------------------- |
82e41f4b71Sopenharmony_ci  | change | {<br>&nbsp;checked:true&nbsp;\|&nbsp;false&nbsp;<br>} | checkbox多选框或radio单选框的checked状态发生变化时触发该事件。 |
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci## 方法
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci除支持[通用方法](js-components-common-methods.md)外,还支持如下方法:
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci| 名称                  | 参数                                       | 描述                                       |
89e41f4b71Sopenharmony_ci| ------------------- | ---------------------------------------- | ---------------------------------------- |
90e41f4b71Sopenharmony_ci| focus               | {<br>&nbsp;focus:&nbsp;true\|false<br>&nbsp;},<br>focus不传值时,默认为true。 | 使组件获得或者失去焦点,type为text\|email\|date\|time\|number\|password时,可弹出或收起输入法。 |
91e41f4b71Sopenharmony_ci| showError           | {<br>&nbsp;error:&nbsp;string&nbsp;<br>} | 展示输入错误提示,type为text\|email\|date\|time\|number\|password时生效。 |
92e41f4b71Sopenharmony_ci| delete<sup>6+</sup> | -                                        | type为text\|email\|date\|time\|number\|password时,根据当前光标位置删除文本内容,如果当前输入组件没有光标,默认删除最后一个字符并展示光标。 |
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci## 示例
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci1. type为text
97e41f4b71Sopenharmony_ci   ```html
98e41f4b71Sopenharmony_ci   <!-- xxx.hml -->
99e41f4b71Sopenharmony_ci   <div class="content">
100e41f4b71Sopenharmony_ci     <input id="input" class="input" type="text" value="" maxlength="20" enterkeytype="send"
101e41f4b71Sopenharmony_ci       headericon="/common/search.svg" placeholder="Please input text" onchange="change"
102e41f4b71Sopenharmony_ci       onenterkeyclick="enterkeyClick">
103e41f4b71Sopenharmony_ci     </input>
104e41f4b71Sopenharmony_ci     <input class="button" type="button" value="Submit" onclick="buttonClick" style="color: blue"></input>
105e41f4b71Sopenharmony_ci   </div>
106e41f4b71Sopenharmony_ci   ```
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci   ```css
109e41f4b71Sopenharmony_ci   /* xxx.css */
110e41f4b71Sopenharmony_ci   .content {
111e41f4b71Sopenharmony_ci     width: 100%;
112e41f4b71Sopenharmony_ci     flex-direction: column;
113e41f4b71Sopenharmony_ci     align-items: center;
114e41f4b71Sopenharmony_ci   }
115e41f4b71Sopenharmony_ci   .input {
116e41f4b71Sopenharmony_ci     width: 60%;
117e41f4b71Sopenharmony_ci     placeholder-color: gray;
118e41f4b71Sopenharmony_ci   }
119e41f4b71Sopenharmony_ci   .button {
120e41f4b71Sopenharmony_ci     width: 60%;
121e41f4b71Sopenharmony_ci     background-color: gray;
122e41f4b71Sopenharmony_ci     margin-top: 20px;
123e41f4b71Sopenharmony_ci    }
124e41f4b71Sopenharmony_ci   ```
125e41f4b71Sopenharmony_ci   
126e41f4b71Sopenharmony_ci   ```js
127e41f4b71Sopenharmony_ci   // xxx.js
128e41f4b71Sopenharmony_ci   import promptAction from '@ohos.promptAction'
129e41f4b71Sopenharmony_ci   export default {
130e41f4b71Sopenharmony_ci     change(e){
131e41f4b71Sopenharmony_ci       promptAction.showToast({
132e41f4b71Sopenharmony_ci         message: "value: " + e.value,
133e41f4b71Sopenharmony_ci         duration: 3000,
134e41f4b71Sopenharmony_ci       });
135e41f4b71Sopenharmony_ci     },
136e41f4b71Sopenharmony_ci     enterkeyClick(e){
137e41f4b71Sopenharmony_ci       promptAction.showToast({
138e41f4b71Sopenharmony_ci         message: "enterkey clicked",
139e41f4b71Sopenharmony_ci         duration: 3000,
140e41f4b71Sopenharmony_ci       });
141e41f4b71Sopenharmony_ci     },
142e41f4b71Sopenharmony_ci     buttonClick(e){
143e41f4b71Sopenharmony_ci       this.$element("input").showError({
144e41f4b71Sopenharmony_ci         error: 'error text'
145e41f4b71Sopenharmony_ci       });
146e41f4b71Sopenharmony_ci     },
147e41f4b71Sopenharmony_ci    }
148e41f4b71Sopenharmony_ci   ```
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci   ![zh-cn_image_0000001252835901](figures/zh-cn_image_0000001252835901.png)
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci2. type为button
154e41f4b71Sopenharmony_ci   ```html
155e41f4b71Sopenharmony_ci   <!-- xxx.hml -->
156e41f4b71Sopenharmony_ci   <div class="div-button">
157e41f4b71Sopenharmony_ci     <input class="button" type="button" value="Input-Button"></input>
158e41f4b71Sopenharmony_ci   </div>
159e41f4b71Sopenharmony_ci   ```
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci   ```css
162e41f4b71Sopenharmony_ci   /* xxx.css */
163e41f4b71Sopenharmony_ci   .div-button {
164e41f4b71Sopenharmony_ci     flex-direction: column;
165e41f4b71Sopenharmony_ci     align-items: center;
166e41f4b71Sopenharmony_ci   }
167e41f4b71Sopenharmony_ci   .button {
168e41f4b71Sopenharmony_ci     margin-top: 30px;
169e41f4b71Sopenharmony_ci     width: 280px;
170e41f4b71Sopenharmony_ci   }
171e41f4b71Sopenharmony_ci   ```
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci   ![zh-cn_image_0000001207995958](figures/zh-cn_image_0000001207995958.png)
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci3. type为checkbox
176e41f4b71Sopenharmony_ci   ```html
177e41f4b71Sopenharmony_ci   <!-- xxx.hml -->
178e41f4b71Sopenharmony_ci   <div class="content">
179e41f4b71Sopenharmony_ci     <input onchange="checkboxOnChange" checked="true" type="checkbox"></input>
180e41f4b71Sopenharmony_ci   </div>
181e41f4b71Sopenharmony_ci   ```
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ci   ```css
184e41f4b71Sopenharmony_ci   /* xxx.css */
185e41f4b71Sopenharmony_ci   .content{
186e41f4b71Sopenharmony_ci     width: 100%;
187e41f4b71Sopenharmony_ci     height: 200px;
188e41f4b71Sopenharmony_ci     align-items: center; 
189e41f4b71Sopenharmony_ci     justify-content: center;   
190e41f4b71Sopenharmony_ci   }
191e41f4b71Sopenharmony_ci   ```
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci   ```js
194e41f4b71Sopenharmony_ci   // xxx.js
195e41f4b71Sopenharmony_ci   import promptAction from '@ohos.promptAction'
196e41f4b71Sopenharmony_ci   export default {
197e41f4b71Sopenharmony_ci     checkboxOnChange(e) {
198e41f4b71Sopenharmony_ci       promptAction.showToast({
199e41f4b71Sopenharmony_ci         message:'checked: ' + e.checked,
200e41f4b71Sopenharmony_ci         duration: 3000,
201e41f4b71Sopenharmony_ci       });
202e41f4b71Sopenharmony_ci     }
203e41f4b71Sopenharmony_ci   }
204e41f4b71Sopenharmony_ci   ```
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ci   ![zh-cn_image_0000001208155956](figures/zh-cn_image_0000001208155956.png)
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci4. type为radio
209e41f4b71Sopenharmony_ci   ```html
210e41f4b71Sopenharmony_ci   <!-- xxx.hml -->
211e41f4b71Sopenharmony_ci   <div class="content">
212e41f4b71Sopenharmony_ci     <input type="radio" checked='true' name="radioSample" value="radio1" onchange="onRadioChange('radio1')"></input>
213e41f4b71Sopenharmony_ci     <input type="radio" checked='false' name="radioSample" value="radio2" onchange="onRadioChange('radio2')"></input>
214e41f4b71Sopenharmony_ci     <input type="radio" checked='false' name="radioSample" value="radio3" onchange="onRadioChange('radio3')"></input>
215e41f4b71Sopenharmony_ci   </div>
216e41f4b71Sopenharmony_ci   ```
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci   ```css
219e41f4b71Sopenharmony_ci   /* xxx.css */
220e41f4b71Sopenharmony_ci   .content{
221e41f4b71Sopenharmony_ci     width: 100%;
222e41f4b71Sopenharmony_ci     height: 200px;
223e41f4b71Sopenharmony_ci     justify-content: center;
224e41f4b71Sopenharmony_ci     align-items: center;
225e41f4b71Sopenharmony_ci   }
226e41f4b71Sopenharmony_ci   ```
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci   ```js
229e41f4b71Sopenharmony_ci   // xxx.js
230e41f4b71Sopenharmony_ci   import promptAction from '@ohos.promptAction'
231e41f4b71Sopenharmony_ci   export default {
232e41f4b71Sopenharmony_ci     onRadioChange(inputValue, e) {
233e41f4b71Sopenharmony_ci       if (inputValue === e.value) {
234e41f4b71Sopenharmony_ci         promptAction.showToast({
235e41f4b71Sopenharmony_ci           message: 'The chosen radio is ' + e.value,
236e41f4b71Sopenharmony_ci           duration: 3000,
237e41f4b71Sopenharmony_ci         });
238e41f4b71Sopenharmony_ci       }
239e41f4b71Sopenharmony_ci     }
240e41f4b71Sopenharmony_ci   }
241e41f4b71Sopenharmony_ci   ```
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ci   ![zh-cn_image_0000001208315918](figures/zh-cn_image_0000001208315918.png)
244