ui-ts-basic-components-button.md 5.1 KB
Newer Older
E
ester.zhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
# Button


The **\<Button>** component is usually activated by user clicks to perform a specific action, for example, submitting a form. Buttons are classified as capsule, circle, or normal buttons.


## Creating a Button

You can create a button by invoking either of the following APIs:

- Button(options?: {type?: ButtonType, stateEffect?: boolean})
  This API creates a button that contains child components. In the syntax, **type** indicates the button type, and **stateEffect** indicates whether to enable the click effect for the button.

  
  ```
  Button({ type: ButtonType.Normal, stateEffect: true }) {
    Row() {
      Image($r('app.media.loading')).width(20).height(20).margin({ left: 12 })
      Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
    }.alignItems(VerticalAlign.Center)
  }.borderRadius(8).backgroundColor(0x317aff).width(90)
  ```

  ![en-us_image_0000001260555857](figures/en-us_image_0000001260555857.png)

- Button(label?: string, options?: { type?: ButtonType, stateEffect?: boolean })
  This API creates a button that does not contain any child components. In the syntax, **label** indicates whether the created button contains child components.

  
  ```
  Button('Ok', { type: ButtonType.Normal, stateEffect: true })
    .borderRadius(8)
    .backgroundColor(0x317aff)
    .width(90)
  ```

  ![en-us_image_0000001215796030](figures/en-us_image_0000001215796030.png)


## Setting the Button Type

Use the **type** parameter to set the button type to **Capsule**, **Circle**, or **Normal**.

- Capsule button (default type)
  
  ```
  Button('Disable', { type: ButtonType.Capsule, stateEffect: false })
    .backgroundColor(0x317aff)
    .width(90)
  ```

  ![en-us_image_0000001215645452](figures/en-us_image_0000001215645452.png)

- Circle button
  
  ```
  Button('Circle', { type: ButtonType.Circle, stateEffect: false })
    .backgroundColor(0x317aff)
    .width(90)
    .height(90)
  ```

  ![en-us_image_0000001215965420](figures/en-us_image_0000001215965420.png)


## Setting Styles

- Set the border radius:
  In general cases, you can use universal attributes to define the button styles. For example, you can use the **borderRadius** attribute to set the border radius.

  
  ```
  Button('circle border', { type: ButtonType.Normal })
    .borderRadius(20)
  ```

  ![zh-cn_image_0000001190463780](figures/zh-cn_image_0000001190463780.png)

- Setting Text Styles
  Add a font style for text displayed on the button.

  
  ```
  Button('font style', { type: ButtonType.Normal })
    .fontSize(20)
    .fontColor(Color.Red)
    .fontWeight(800)
  ```

  ![zh-cn_image_0000001189744672](figures/zh-cn_image_0000001189744672.png)

- Set the background color:
  You can do so by adding the **backgroundColor** attribute.

  
  ```
  Button('background color').backgroundColor(0xF55A42)
  ```

  ![en-us_image_0000001235146483](figures/en-us_image_0000001235146483.png)

- Assign a function to the button:
  In this example, we are creating a button with the delete function.

  
  ```
  Button({ type: ButtonType.Circle, stateEffect: true }) {
    Image($r('app.media.ic_public_delete_filled')).width(30).height(30)
  }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)
  ```

  ![en-us_image_0000001260373911](figures/en-us_image_0000001260373911.png)


## Adding Events

The **\<Button>** component is usually used to trigger actions. You can bind the **onClick** event to the button to have it respond with custom behavior after being clicked.


```
Button('Ok', { type: ButtonType.Normal, stateEffect: true })
  .onClick(()=>{
    console.info('Button onClick')
  })
```


## Example Scenarios

- Using the Button for Startup

  You can use the button for any UI element that involves the startup operation. The button triggers the predefined event based on the user's operation. For example, you can use a button in the **\<List>** container to redirect the user to another page.

  ```
  import router from '@ohos.router'
  
  @Entry
  @Component
  struct ButtonCase1 {
    build() {
      List({ space: 4 }) {
        ListItem() {
          Button("First").onClick(() => {
            router.push({ url: 'xxx' })
          })
        }
  
        ListItem() {
          Button("Second").onClick(() => {
            router.push({ url: 'yyy' })
          })
        }
  
        ListItem() {
          Button("Third").onClick(() => {
            router.push({ url: 'zzz' })
          })
        }
      }
      .listDirection(Axis.Vertical)
      .backgroundColor(0xDCDCDC).padding(20)
    }
  }
  ```


  ![en-us_image_0000001235626467](figures/en-us_image_0000001235626467.png)


- Using the Button for Submitting Forms
  On the user login/registration page, you can use a button to submit a login or registration request.

  
  ```
  @Entry
  @Component
  struct ButtonCase2 {
    build() {
      Column() {
        TextInput({ placeholder: 'input your username' }).margin({ top: 20 })
        TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 })
        Button('Register').width(300).margin({ top: 20 })
      }.padding(20)
    }
  }
  ```
  
  ![en-us_image_0000001190466492](figures/en-us_image_0000001190466492.png)