harmony 鸿蒙Text Input (TextInput/TextArea)

  • 2023-06-24
  • 浏览 (746)

Text Input (TextInput/TextArea)

The <TextInput> and <TextArea> components are input components typically used to accept input from the user, such as comments, chat messages, and table content. They can be used in combination with other components to meet more diversified purposes, for example, login and registration. For details, see TextInput and TextArea.

Creating a Text Box

The <TextInput> component provides single-line text input, while the <TextArea> component provides multi-line text input. To create these components, use the following APIs:

TextInput(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextInputController})
TextArea(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextAreaController})
  • Single-line text box
  TextInput()

en-us_image_0000001511580844

  • Multi-line text box
  TextArea()

en-us_image_0000001562940481

The <TextArea> component automatically wraps text so that each line does not have more than the width of the component.

  TextArea({text:"I am TextArea I am TextArea I am TextArea"}).width(300)

en-us_image_0000001511580836

Setting the Input Box Type

The <TextInput> component comes in five types. You can specify its type by setting the type parameter to any of the following: Normal, Password, Email, Number, and PhoneNumber.

  • Normal type (default type)
  TextInput()
    .type(InputType.Normal)

en-us_image_0000001562820765

  • Password type
  TextInput()
    .type(InputType.Password)

en-us_image_0000001511580840

Setting Styles

  • Set the placeholder text displayed when there is no input. TextInput({placeholder:‘I am placeholder text’})
  TextInput({placeholder:'I am placeholder text'})

en-us_image_0000001511900400

  • Set the current text input.
  TextInput({placeholder:'I am placeholder text',text:'I am current text input'})

en-us_image_0000001562820761

  • Use backgroundColor to set the background color of the text box.
  TextInput({placeholder:'I am placeholder text',text:'I am current text input'})
    .backgroundColor(Color.Pink)

en-us_image_0000001511740444

More styles can be implemented by leveraging the universal attributes.

Adding Events

You can add the onChange event for the text box to obtain its content changes. You can also add the universal events to implement user interactions.

TextInput()
  .onChange((value: string) => {
    console.info(value);
  })
  .onFocus(() => {
    console.info ('Get Focus');
  })

Example Scenario

In this example, the text box is used to submit forms on the user login or registration page.

@Entry
@Component
struct TextInputSample {
  build() {
    Column() {
      TextInput({ placeholder: 'input your username' }).margin({ top: 20 })
        .onSubmit((EnterKeyType)=>{
          console.info(EnterKeyType+'Enter key type')
        })
      TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 })
        .onSubmit((EnterKeyType)=>{
          console.info(EnterKeyType+'Enter key type')
        })
      Button('Sign in').width(150).margin({ top: 20 })
    }.padding(20)
  }
}

TextInputGIF

你可能感兴趣的鸿蒙文章

harmony 鸿蒙UI Development

harmony 鸿蒙Animation Smoothing

harmony 鸿蒙Animation Overview

harmony 鸿蒙Property Animation APIs

harmony 鸿蒙Property Animation Overview

harmony 鸿蒙Blur Effect

harmony 鸿蒙Color Effect

harmony 鸿蒙Button

harmony 鸿蒙Custom Dialog Box (CustomDialog)

harmony 鸿蒙Progress Indicator (Progress)

0  赞