用户3116712
用户3116712
  • 发布:56 分钟前
  • 更新:56 分钟前
  • 阅读:6

输入组件获焦时实现提示文本的动态效果

分类:uni-app

问题现象
在使用TextInput和TextArea组件进行输入时,如何实现提示文字的动态效果?

背景知识
组件的某些通用属性变化时,可以通过animation属性动画实现渐变过渡效果,提升用户体验。
TextInput是HarmonyOS提供的一种单行文本输入框组件。
TextArea是多行文本输入框组件,当输入的文本内容超过组件宽度时会自动换行显示。
attributeModifier接口支持动态属性设置,支持开发者在属性设置时使用if/else语法,且根据需要使用多态样式设置属性。
解决方案
在Stack组件中创建Text提示文本和TextInput输入框。
创建TextModifier类,实现attributeModifier接口,该类中的applyNormalAttribute方法可根据对应的boolean类型的值动态修改对应组件的样式属性。
当点击TextInput组件时,执行onFocus方法,若文本值为空,将对应的TextModifier类的属性装载至该组件上,从而实现animation动画效果。
完整示例参考如下:

@Entry
@Component
struct TextInputTestOne {
// 定义状态变量
message: string = 'Hello World';
textAreaValue: string = '';
textInputValue: string = '';
selectValue: string = '';
@State modifierOne: TextModifier = new TextModifier(50, 150, 20);
@State modifierTwo: TextModifier = new TextModifier(50, 0, 20);
opacityOne: number = 0.3;
opacityTwo: number = 0.3;

build() {
Column() {
Column() {
Stack() {
Text('请在这输入文字:')
.fontSize(this.modifierOne.fontSize)
.textAlign(TextAlign.Start)
.fontColor(Color.Black)
.margin({ left: this.modifierOne.marginLeft, bottom: this.modifierOne.marginBottom })
.attributeModifier(this.modifierOne)
.width('100%')
.opacity(this.opacityOne)
// 实现动画效果
.animation({
duration: 300,
curve: Curve.Friction,
iterations: 1,
playMode: PlayMode.Normal
});
TextArea({ text: $$this.textAreaValue })
// 聚焦回调时间
.onFocus(() => {
if (this.textAreaValue === '') {
this.modifierOne.isFocus = true;
this.opacityOne = 1;
}
})
// 失焦回调事件
.onBlur(() => {
if (this.textAreaValue === '') {
this.modifierOne.isFocus = false;
this.opacityOne = 0.3;
}
})
.height('80%');
};
}
.justifyContent(FlexAlign.Center)
.height('30%')
.margin({ left: 20, right: 20 });

  Column() {  
    Stack() {  
      Text('请在这输入文字:')  
        .fontSize(this.modifierTwo.fontSize)  
        .fontColor(Color.Black)  
        .textAlign(TextAlign.Start)  
        .width('100%')  
        .margin({ left: this.modifierTwo.marginLeft, bottom: this.modifierTwo.marginBottom })  
        .attributeModifier(this.modifierTwo)  
        .opacity(this.opacityTwo)  
        // 实现动画效果  
        .animation({  
          duration: 300,  
          curve: Curve.Friction,  
          iterations: 1,  
          playMode: PlayMode.Normal  
        });  
      TextInput({ text: $$this.textInputValue })  
      // 聚焦回调时间  
        .onFocus(() => {  
          if (this.textInputValue === '') {  
            this.modifierTwo.isFocus = true;  
            this.opacityTwo = 1;  
          }  
        })  
        // 失焦回调事件  
        .onBlur(() => {  
          if (this.textInputValue === '') {  
            this.modifierTwo.isFocus = false;  
            this.opacityTwo = 0.3;  
          }  
        });  
    };  
  }  
  .justifyContent(FlexAlign.Center)  
  .height('30%')  
  .margin({ left: 20, right: 20 });  
}  
.height('100%')  
.width('100%');  

}
}

class TextModifier implements AttributeModifier<TextAttribute> {
isFocus: boolean | undefined = undefined;
marginLeft: number = 0;
marginBottom: number = 0;
fontSize: number = 0;

// 绑定属性
applyNormalAttribute(instance: TextAttribute): void {
if (this.isFocus) {
this.marginLeft -= 10;
this.marginBottom += 40;
this.fontSize -= 7;
instance
.fontSize(this.fontSize)
.margin({ left: this.marginLeft, bottom: this.marginBottom });
} else if (this.isFocus === false) {
this.marginLeft += 10;
this.marginBottom -= 40;
this.fontSize += 7;
instance
.fontSize(this.fontSize)
.margin({ left: this.marginLeft, bottom: this.marginBottom });
}
}

constructor(marginLeft: number, marginBottom: number, fontSize: number) {
this.marginLeft = marginLeft;
this.marginBottom = marginBottom;
this.fontSize = fontSize;
}
}
https://coub.com/view/4b7gqt
https://coub.com/view/4b7gqq
https://coub.com/view/4b7gql
https://coub.com/view/4b7gqi
https://coub.com/view/4b7gqg
https://coub.com/view/4b7gqe
https://coub.com/view/4b7gqb
https://coub.com/view/4b7gq9
https://coub.com/view/4b7gq6
https://coub.com/view/4b7gq3
https://coub.com/view/4b7gq1
https://coub.com/view/4b7gpz
https://coub.com/view/4b7gpw
https://coub.com/view/4b7gpe
https://coub.com/view/4b7gpa
https://coub.com/view/4b7gp8
https://coub.com/view/4b7gp7
https://coub.com/view/4b7gp6
https://coub.com/view/4b7gp5
https://coub.com/view/4b7gp1
https://coub.com/view/4b7gow
https://coub.com/view/4b7gou
https://coub.com/view/4b7goq
https://coub.com/view/4b7goj
https://coub.com/view/4b7goh
https://coub.com/view/4b7gog
https://coub.com/view/4b7go2
https://coub.com/view/4b7gnz
https://coub.com/view/4b7gnf
https://coub.com/view/4b7gne
https://coub.com/view/4b7gnd
https://coub.com/view/4b7gnb
https://coub.com/view/4b7gmd
https://coub.com/view/4b7gmb
https://coub.com/view/4b7gm6
https://coub.com/view/4b7gm1
https://coub.com/view/4b7glo
https://coub.com/view/4b7glk
https://coub.com/view/4b7glh
https://coub.com/view/4b7glg
https://coub.com/view/4b7glf
https://coub.com/view/4b7glc
https://coub.com/view/4b7glb
https://coub.com/view/4b7gl9
https://coub.com/view/4b7gl4
https://coub.com/view/4b7gl1
https://coub.com/view/4b7gkp
https://coub.com/view/4b7gkh
https://coub.com/view/4b7gk8
https://coub.com/view/4b7gk2
https://coub.com/view/4b7gju
https://coub.com/view/4b7gjn
https://coub.com/view/4b7gjl
https://coub.com/view/4b7gjj
https://coub.com/view/4b7gji
https://coub.com/view/4b7gjg
https://coub.com/view/4b7gjf
https://coub.com/view/4b7gje
https://coub.com/view/4b7gjb
https://coub.com/view/4b7gj7
https://coub.com/view/4b7gj5
https://coub.com/view/4b7gj2
https://coub.com/view/4b7giy
https://coub.com/view/4b7giw
https://coub.com/view/4b7giv
https://coub.com/view/4b7giu
https://coub.com/view/4b7gis
https://coub.com/view/4b7giq
https://coub.com/view/4b7gio
https://coub.com/view/4b7gil
https://coub.com/view/4b7gij
https://coub.com/view/4b7gie
https://coub.com/view/4b7gic
https://coub.com/view/4b7gia
https://coub.com/view/4b7gi7
https://coub.com/view/4b7gi2
https://coub.com/view/4b7ghx
https://coub.com/view/4b7ghi
https://coub.com/view/4b7ghh
https://coub.com/view/4b7ghd
https://coub.com/view/4b7ggo
https://coub.com/view/4b7ggn
https://coub.com/view/4b7ggl
https://coub.com/view/4b7ggg
https://coub.com/view/4b7gge
https://coub.com/view/4b7ggd
https://coub.com/view/4b7ggc
https://coub.com/view/4b7ggb
https://coub.com/view/4b7gga
https://coub.com/view/4b7gg7
https://coub.com/view/4b7gg1
https://coub.com/view/4b7gfw
https://coub.com/view/4b7gft
https://coub.com/view/4b7gfs
https://coub.com/view/4b7gfq
https://coub.com/view/4b7gfm
https://coub.com/view/4b77kh
https://coub.com/view/4b77kd
https://coub.com/view/4b77ka
https://coub.com/view/4b77k8
https://coub.com/view/4b77k4
https://coub.com/view/4b77k2
https://coub.com/view/4b77k0
https://coub.com/view/4b77jz
https://coub.com/view/4b77ju
https://coub.com/view/4b77jk
https://coub.com/view/4b77jf
https://coub.com/view/4b77ja
https://coub.com/view/4b77j1
https://coub.com/view/4b77iz

0 关注 分享

要回复文章请先登录注册