openharmony 鸿蒙 js-lite-components-basic-input

2025-06-12 浏览 (1)

input

The <input> component provides an interactive interface to receive user input. It can be a radio button, check box, or button.

NOTE

This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.

Child Components

Not supported

Attributes

NameTypeDefault ValueMandatoryDescription
typestring
button
NoType of the component, which cannot be dynamically modified. The options are as follows:
- button: a button that can be clicked.
- checkbox: a check box.
- radio: a radio button that allows users to select one from multiple others with the same name.
checkedbooleanfalseNoWhether the component is selected. This attribute is valid only when type is set to checkbox or radio.
namestring-NoName of the component.
valuestring-NoValue of the component. When type is radio, this attribute is mandatory and the value must be unique for radio buttons with the same name.
idstring-NoUnique ID of the component.
stylestring-NoStyle declaration of the component.
classstring-NoStyle class of the component, which is used to refer to a style table.
refstring-NoReference information of child elements, which is registered with the parent component on $refs.

Events

  • When type is set to checkbox or radio, the following events are supported. |Name|Parameter|Description| |--------|--------|--------| |change|{ checked:true |false }|Triggered when the checked status of the checkbox or radio button changes.| |click|-|Triggered when the component is clicked.| |longpress|-|Triggered when the component is long pressed.| |swipe5+|SwipeEvent|Triggered when a user quickly swipes on the component.|

  • When type is set to button, the following events are supported. |Name|Parameter|Description| |--------|--------|--------| |click|-|Triggered when the component is clicked.| |longpress|-|Triggered when the component is long pressed.| |swipe5+|SwipeEvent|Triggered when a user quickly swipes on the component.|

Styles

NameTypeDefault ValueMandatoryDescription
color<color>#ffffffNoText color of the component.
font-size<length>30pxNoFont size of the component.
width<length>-NoWidth of the component. The default value for a button is 100px.
height<length>-NoHeight of the component. The default value for a button is 50px.
font-familystringSourceHanSansSC-RegularNoFont. Only the SourceHanSansSC-Regular font is supported.
padding<length>0NoShorthand attribute to set the padding for all sides.
The attribute can have one to four values:
- If you set only one value, it specifies the padding for all the four sides.
- If you set two values, the first value specifies the top and bottom padding, and the second value specifies the left and right padding.
- If you set three values, the first value specifies the top padding, the second value specifies the left and right padding, and the third value specifies the bottom padding.
- If you set four values, they respectively specify the padding for top, right, bottom, and left sides (in clockwise order).
padding-[left|top|right|bottom]<length>0NoLeft, top, right, and bottom padding.
margin<length> |<percentage>5+0NoShorthand attribute to set the margin for all sides. The attribute can have one to four values:
- If you set only one value, it specifies the margin for all the four sides.
- If you set two values, the first value specifies the top and bottom margins, and the second value specifies the left and right margins.
- If you set three values, the first value specifies the top margin, the second value specifies the left and right margins, and the third value specifies the bottom margin.
- If you set four values, they respectively specify the margin for top, right, bottom, and left sides (in clockwise order).
margin-[left|top|right|bottom]<length> |<percentage>5+0NoLeft, top, right, and bottom margins.
border-width<length>0NoShorthand attribute to set the margin for all sides.
border-color<color>blackNoShorthand attribute to set the color for all borders.
border-radius<length>-NoRadius of round-corner borders.
background-color<color>-NoBackground color.
displaystringflexNoHow and whether to display the box containing an element. Available values are as follows:
- flex: flexible layout
- none: not rendered
[left|top]<length> |<percentage>6+-NoEdge of the element.
- left: left edge position of the element. This attribute defines the offset between the left edge of the margin area of a positioned element and left edge of its containing block.
- top: top edge position of the element. This attribute defines the offset between the top edge of a positioned element and that of a block included in the element.

Example

  1. Common button

    <!-- xxx.hml -->
    <div class="div-button">
      <input class="button" type="button" value="Input-Button"></input>
    </div>
    
    /* xxx.css */
    .div-button {
      flex-direction: column;
      align-items: center;
      width: 100%;
      height: 100%;
    }
    .button {
      margin-top: 30px;
      width: 280px;
    }
    

    input-type-button

  2. Check box

    <!-- xxx.hml -->
    <div class="content">
      <input onchange="checkboxOnChange" checked="true" type="checkbox"></input>
      <text class="text">{{text}}</text>
    </div>
    
    /* xxx.css */
    .content{
      width: 100%;
      height: 100%;
      flex-direction: column;
      align-items: center; 
      justify-content: center;   
    }
    .text{
      font-size: 30px;
      text-align: center;
      width: 200px;
      margin-top: 20px;
      height: 100px;
    }
    
    // xxx.js
    export default {
      data: {
        text: "text"
      },
      checkboxOnChange(e) {
        this.text = e.checked;
      }
    }
    

    input-type-checkbox

  3. Radio button

    <!-- xxx.hml -->
    <div class="container">
      <div class="item">
        <input type="radio" checked="true" name="radioSample" value="radio1" onchange="onRadioChange"></input>
        <text class="text">radio1</text>
      </div>
      <div class="item">
        <input type="radio" checked="false" name="radioSample" value="radio2" onchange="onRadioChange"></input>
        <text class="text">radio2</text>
      </div>
      <div class="item">
        <input type="radio" checked="false" name="radioSample" value="radio3" onchange="onRadioChange"></input>
        <text class="text">radio3</text>
      </div>
    </div>
    
    /* xxx.css */
    .container {
      width: 100%;
      height: 100%;
      justify-content: center;
      align-items: center;
      flex-direction: column;
    }
    .item {
      width: 50%;
      height: 30%;
      justify-content: center;
    }
    .text {
      margin-top: 25%;
      font-size: 30px;
      text-align: center;
      width: 200px;
      height: 100px;
    }
    

    input-type-radio

你可能感兴趣的鸿蒙文章

harmony 鸿蒙JavaScript-compatible Web-like Development Paradigm (ArkUI.Lite)

harmony 鸿蒙Universal Attributes

harmony 鸿蒙Universal Events

harmony 鸿蒙Universal Styles

harmony 鸿蒙chart

harmony 鸿蒙image-animator

harmony 鸿蒙image

harmony 鸿蒙marquee

harmony 鸿蒙picker-view

harmony 鸿蒙progress

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/BtPIPw