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

HarmonyOS
2025-01-09 15:04:59
650浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

如果自定义的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')
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 17:29:23


相关问题
HarmonyOS Checkbox如何自定义图标
1038浏览 • 0回复 待解决
桌面图标shorcut功能支持自定义吗?
1005浏览 • 1回复 待解决
鸿蒙组件toast自定义样式
9803浏览 • 1回复 待解决
HarmonyOS如何自定义视频组件样式
1204浏览 • 1回复 待解决
HarmonyOS 自定义Slider样式
1228浏览 • 1回复 待解决
HarmonyOS TextInput自定义键盘
1289浏览 • 1回复 待解决
HarmonyOS 自定义相机预览拉伸问题
855浏览 • 1回复 待解决
HarmonyOS Radio设置自定义单选样式
891浏览 • 1回复 待解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。