Search组件不支持设置placeholder文本和搜索图标的间距,可以自定义搜索框组件实现该效果:
@Entry @Component struct searchTest { @State searchText: string = ''; build() { Row() { Column({ space: 10 }) { Row({ space: 5 }) { Row() { Image($r('sys.media.ohos_ic_public_search_filled')) .size({ width: 18, height: 18 }) .opacity(0.5) .margin({ left: 10, right: 0 }) TextInput({ placeholder: '搜索', text: this.searchText }) .padding({ left: 4 }) .layoutWeight(1) .backgroundColor(Color.Transparent) .height('100%') } .layoutWeight(1) .backgroundColor('#f2f3f5') .borderRadius('50%') }.width('100%') .height(40) } .width('100%') .padding({ top: 20, left: 20, right: 20 }) .height('100%') } .height('100%') .backgroundColor(Color.White) } }
Q:如何设置Search组件删除按钮颜色?
A:参考示例设置symbol类型清除按钮。
Q:Search组件点击旁边的‘x’号会触发什么回调?
A:会触发onChange()、onWillChange()、onTextSelectionChange等回调。