智能客服
你问我答,随时在线为你解决问题
在使用TextInput和TextArea组件进行输入时,如何实现提示文字的动态效果?
完整示例参考如下:
@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;
}
} 效果图如下:
