HarmonyOS鸿蒙Next中TextInput组件密码模式下,右边的眼睛图标自定义,怎么设置自定义图标的样式?现在默认图标会固定拉伸

HarmonyOS鸿蒙Next中TextInput组件密码模式下,右边的眼睛图标自定义,怎么设置自定义图标的样式?现在默认图标会固定拉伸 TextInput组件密码模式下,右边的眼睛图标自定义,怎么设置自定义图标的样式?现在默认图标会固定拉伸

3 回复

如果自定义的icon出现拉伸问题,使用stack容器作为父容器,子容器使用image来实现自定义一个passwordIcon,此时即可对Image组件的位置、大小、颜色做出更改。

参考代码:

@Entry
@Component
struct TextInputExample {
  @State text: string = ''
  @State changeType: InputType = InputType.Password
  @State isVisible: boolean = false
  @State changeState: boolean = false
  controller: TextInputController = new TextInputController()

  build() {
    Column() {
      Flex({direction: FlexDirection.Row}){
        Stack(){
          TextInput({ text: this.text, controller: this.controller })
            .type(this.changeType)
            .placeholderFont({ size: 16, weight: 400 })
            .showPasswordIcon(false)
            .width(336)
            .height(56)
            //设置内间距让输入内容不超过图标位置
            .padding({
              right: 50
            })
            .onChange((value: string) => {
              this.text = value
            })
          //Image覆盖passwordIcon实现
          Image($r(this.isVisible?'app.media.visible':'app.media.Invisible'))
            .margin({
              left: 280
              // left: 200
            })
            .backgroundColor('#E7E8EA')
            .width(20)
            .height(20)
            .onClick(() =>{
              this.changeState = !this.changeState
              this.isVisible = !this.isVisible
              if(this.changeState){
                this.changeType = InputType.Normal
              } else {
                this.changeType = InputType.Password
              }
            })
        }
      }
    }.width('100%').height('100%').backgroundColor('#F1F3F5')
  }
}

规格上自定义图标的样式目前是固定的,暂时不支持相关属性去修改,后续可关注官网的更新。

更多关于HarmonyOS鸿蒙Next中TextInput组件密码模式下,右边的眼睛图标自定义,怎么设置自定义图标的样式?现在默认图标会固定拉伸的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,TextInput组件的密码模式下,默认的眼睛图标可以通过自定义样式进行修改。要实现自定义图标样式,可以通过设置rightIcon属性来指定自定义的图标资源。具体步骤如下:

  1. 准备自定义图标资源:在项目的resources目录下,放置自定义的图标文件,例如eye_custom.png

  2. 在XML布局文件中,使用TextInput组件,并设置rightIcon属性为自定义图标资源:

<TextInput
    ohos:id="$+id:text_input"
    ohos:width="match_parent"
    ohos:height="match_content"
    ohos:rightIcon="$media:eye_custom"
    ohos:inputType="password"/>
  1. 如果需要进一步调整图标的大小或样式,可以通过ElementsetRightIconSizesetRightIconTint方法进行设置:
TextInput textInput = (TextInput) findComponentById(ResourceTable.Id_text_input);
textInput.setRightIconSize(50); // 设置图标大小为50px
textInput.setRightIconTint(Color.BLUE); // 设置图标颜色为蓝色

通过以上步骤,可以实现自定义图标样式,并避免默认图标的固定拉伸问题。

在HarmonyOS鸿蒙Next中,可以通过TextInput组件的rightIcon属性自定义密码模式下的右侧图标样式。你可以使用Image组件设置自定义图标,并通过style属性调整图标大小、颜色等样式,避免默认图标的固定拉伸问题。示例代码:

TextInput({ type: InputType.Password })
  .rightIcon(
    Image($r('app.media.custom_icon'))
      .width(24)
      .height(24)
      .tintColor(Color.Black)
  )

通过这种方式,可以灵活控制图标样式。

回到顶部