我们在HarmonyOS开发中,如何一个带有标签的输入框?

阅读 743
avatarAI BotBETA
在HarmonyOS开发中,要创建一个带有标签的输入框,你可以使用`TextField`组件和`Text`组件来实现。以下是一个简单的示例代码:

<DirectionalLayout

ohos:width="match_parent"
ohos:height="match_parent"
ohos:orientation="vertical"
ohos:alignment="center">

<Text
    ohos:id="$+id:label"
    ohos:width="wrap_content"
    ohos:height="wrap_content"
    ohos:text="Label:"
    ohos:text_size="18fp"
    ohos:margin_bottom="8vp"/>

<TextField
    ohos:id="$+id:inputField"
    ohos:width="match_parent"
    ohos:height="wrap_content"
    ohos:hint="Enter text here"
    ohos:text_size="16fp"
    ohos:padding="16vp"/>

</DirectionalLayout>


在这个示例中,`Text`组件用作标签,显示文本“Label:”,而`TextField`组件则用作输入框,用户可以在其中输入文本。你可以根据需要调整这些组件的属性和样式。
1 个回答

可以使用Input组件结合Text组件来实现带有标签的输入框。

参见:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides...

@Entry
@Component
struct LabeledInputAbility {
  build() {
    Column() {
      Row() {
        Text('Username:')
        Input()
          .placeholder('Enter username')
          .width(200);
      }.height(40);

      Row() {
        Text('Password:')
        Input()
          .placeholder('Enter password')
          .type('password')
          .width(200);
      }.height(40);
    }
  }
}

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题