开发者使用cancelButton属性时,有以下经典场景:
@Entry
@Component
struct Index {
@State isFocus: boolean = false;
build() {
Column() {
Stack({ alignContent: Alignment.End }) {
Search({ placeholder: '请输入搜索内容' })
.id('search_001')
.height(44)
.width('100%')
.defaultFocus(false)
.backgroundColor(0xFFF7F7F7)
.textAlign(TextAlign.Start)
.copyOption(CopyOptions.None)
.placeholderFont({ size: 16 })
.textFont({ size: 16 })
.fontColor(Color.Black)
.borderRadius(16)
.layoutWeight(1)
.border({
width: 1,
color: this.isFocus ? Color.Red : Color.Transparent,
style: BorderStyle.Solid
})
.caretStyle({ width: 2, color: Color.Red })
.cancelButton({
style: CancelButtonStyle.INPUT,
icon: {
size: 80,
src: $r('app.media.startIcon')
}
});
Image($r('app.media.background'))
.height(44)
.onClick(() => {
// 此处编写自定义逻辑。
this.isFocus = !this.isFocus;
});
};
};
}
} @Entry
@Component
struct ClearNodeExample {
@State text: string = '';
// 编辑状态
@State isOK: boolean = false;
controller: TextInputController = new TextInputController();
build() {
Column() {
TextInput({ placeholder: 'input ...', controller: this.controller })
.width(350)
.height(60)
.margin({ bottom: 16, left: 20, right: 20 })
.cancelButton({
// 使用三元表达式更改图标的显示模式
style: (this.isOK ? CancelButtonStyle.INPUT : CancelButtonStyle.INVISIBLE),
icon: {
size: 45,
src: $r('app.media.startIcon'),
color: Color.Blue
}
})
.onEditChange((isEditing: boolean) => {
// 存储TextInput组件的编辑状态
this.isOK = isEditing;
})
.onChange((value: string) => {
this.text = value;
});
TextInput({ placeholder: 'input ...', controller: this.controller })
.width(350)
.height(60)
.margin({ left: 20, right: 20 })
.cancelButton({
icon: {
size: 45,
src: $r('app.media.startIcon'),
color: Color.Blue
}
});
};
}
} Q:TextInput组件的cancelButton的内边距如何取消?
A:为了使cancelButton的图标在视觉上更贴近TextInput组件的最右侧,可以使用Stack布局组件来实现图标的覆盖效果。以下是一个示例代码,展示了如何通过Stack来调整cancelButton的位置:
@Entry
@Component
struct TextInputPage {
@State flag: boolean = false;
@State isOK: boolean = false;
@State text: string = '';
build() {
Column() {
Stack() {
TextInput({ placeholder: 'input ...', })
.width(350)
.height(60)
.margin({ bottom: 16, left: 20, right: 20 })
.cancelButton({
style: (CancelButtonStyle.INPUT),
icon: {
size: 0,
src: $r('app.media.startIcon'),
color: Color.Blue
}
})
.onEditChange((isEditing: boolean) => {
this.flag = isEditing;
this.isOK = this.flag;
})
.onChange((value: string) => {
this.text = value;
});
if (this.isOK) {
Image($r('app.media.startIcon')).height(50).width(50).margin({ bottom: 16, left: 318, right: 20 })
.onClick(() => {
});
}
};
}
.height('100%')
.width('100%');
}
} Q:如何实现TextInput的清除按钮功能?
A:在API11及以上版本可以设置右侧清除按钮样式;在API18及以上版本可以设置symbol类型清除按钮。