仙银|
UI
如何实现聊天中的表情面板
支持 emoji 表情和图片表情
1
浏览86 发布于2025-12-19 11:11四川
全部评论
最多点赞
最新发布
最早发布


预览效果:

使用 Swiper + Grid 实现表情面板
1、定义懒加载DataSource
收起
自动换行
深色代码主题
复制
export abstract class BaseDataSource<T> implements IDataSource {
private listeners: DataChangeListener[] = []
public abstract totalCount(): number
public abstract getData(index: number): T
registerDataChangeListener(listener: DataChangeListener): void {
if (this.listeners.indexOf(listener) < 0) {
this.listeners.push(listener);
}
}
unregisterDataChangeListener(listener: DataChangeListener): void {
const pos = this.listeners.indexOf(listener)
if (pos >= 0) {
this.listeners.splice(pos, 1);
}
}
notifyDataChangeAll(): void {
const total = this.totalCount()
for (let i = 0; i < total; i++) {
this.notifyDataChange(i)
}
}
notifyDataAdd(index: number, count: number = 1): void {
this.notifyDatasetChange([{ type: DataOperationType.ADD, index: index, count: count }])
}
notifyDataChange(index: number, key?: string): void {
this.notifyDatasetChange([{ type: DataOperationType.CHANGE, index: index, key: key }])
}
notifyDataDelete(index: number, count: number = 1): void {
this.notifyDatasetChange([{ type: DataOperationType.DELETE, index: index, count: count }])
}
notifyDataMove(from: number, to: number): void {
this.notifyDatasetChange([{ type: DataOperationType.MOVE, index: { from: from, to: to } }])
}
notifyDatasetChange(operations: DataOperation[]): void {
this.listeners.forEach(listener => {
listener.onDatasetChange(operations);
})
}
notifyDataReload() {
this.notifyDatasetChange([{ type: DataOperationType.RELOAD }])
}
}
2、实现emoji表情view
收起
自动换行
深色代码主题
复制
/**
* @fileName : EmojiView.ets
* @author : @cxy
* @date : 2025/12/19
* @description : emoji 组件
*/
import { BaseDataSource } from "./BaseDataSource";
function emojiList(): string[] {
return [
"😎", "😍", "😘", "😚", "🙂", "🤗", "🤔", "😑",
"🙄", "😏", "😣", "😥", "🤐", "😯", "😪", "😫",
"😴", "😜", "😝", "🤤", "😳", "🤭", "😂", "😄",
"😭", "❤️", "🤗", "🌞", "🀄", "💰", "👌", "🤞",
"🐮", "🍀", "😁", "🤣", "😅", "😆", "😉", "😊",
"😋", "😓", "😒", "😔", "😕", "🙃", "🤑", "😲",
"👍", "🙁", "😖", "😤", "😢", "😨", "😩", "😬",
"😰", "😱", "😵", "😡", "😠", "💪", "🤝", "🤩",
"🥰", "💝", "🌷", "🌼", "🌻", "🌺", "💯", "🙏",
"🏆", "🌛", "💣", "🚀", "🎉", "😷", "🤒", "🤕",
"🤢", "🤧", "😇", "🤠", "🍺", "🌹", "🤡", "🤥",
"🤓", "😈", "👹", "👺", "💀", "👻", "👽", "🤖",
"💩", "😾", "😸", "😹", "😻", "😼", "😽", "🙀",
"😿"
]
}
export class EmojiDataSource extends BaseDataSource<string> {
list: string[] = [];
public totalCount(): number {
return this.list.length;
}
public getData(index: number): string {
return this.list[index];
}
public initList(data: string[]): void {
this.list.push(...data);
this.notifyDataAdd(0, this.list.length)
}
}
@Component
export default struct EmojiView {
@Link enableDel: boolean
onSelect?: (emoji: string) => void
onDelete?: () => void
private dataSource: EmojiDataSource = new EmojiDataSource()
aboutToAppear(): void {
this.dataSource.initList(emojiList())
}
build() {
Stack() {
Grid() {
LazyForEach(this.dataSource, (e: string) => {
GridItem() {
Text(e).fontSize(22).textAlign(TextAlign.Center).width('100%').aspectRatio(1)
}.onClick(() => {
this.onSelect?.(e)
})
})
}
.edgeEffect(EdgeEffect.Spring, { alwaysEnabled: true })
.width('100%')
.height('100%')
.cachedCount(16)
.columnsTemplate('1fr '.repeat(8))
.maxCount(this.dataSource.totalCount())
Row() {
Image($r('app.media.chat_em_del'))
.width(16)
.opacity(this.enableDel ? 1 : 0.3)
}
.borderRadius(5)
.backgroundColor('#fff')
.padding({
left: 14,
right: 14,
top: 12,
bottom: 12
})
.offset({ x: -15, y: -10 })
.onClick(() => {
if (this.enableDel) {
this.onDelete?.()
}
})
}
.height('100%')
.width('100%')
.align(Alignment.BottomEnd)
}
}3、实现emotion图片view
收起
自动换行
深色代码主题
复制
/**
* @fileName : EmotionImage.ets
* @author : @cxy
* @date : 2025/12/19
* @description : 表情图片
*/
import { BaseDataSource } from './BaseDataSource';
export class Emotion {
name: string = ''
url: ResourceStr = ''
constructor(name: string, url: ResourceStr) {
this.name = name
this.url = url
}
}
export class EmotionDataSource extends BaseDataSource<Emotion> {
list: Emotion[] = [];
public totalCount(): number {
return this.list.length;
}
public getData(index: number): Emotion {
return this.list[index];
}
public initList(data: Emotion[]): void {
this.list.push(...data);
this.notifyDataAdd(0, this.list.length)
}
}
export const EmotionList: Emotion[] = [
new Emotion('笑脸', $r('app.media.startIcon')),
new Emotion('笑哭', $r('app.media.startIcon')),
new Emotion('捂脸', $r('app.media.startIcon')),
new Emotion('比心', $r('app.media.startIcon')),
new Emotion('加油', $r('app.media.startIcon')),
new Emotion('生气', $r('app.media.startIcon')),
new Emotion('委屈', $r('app.media.startIcon')),
new Emotion('害羞', $r('app.media.startIcon')),
new Emotion('吃瓜', $r('app.media.startIcon')),
new Emotion('狗头', $r('app.media.startIcon')),
new Emotion('点赞', $r('app.media.startIcon')),
new Emotion('发财', $r('app.media.startIcon')),
new Emotion('摆烂', $r('app.media.startIcon'))
];
@Component
export struct EmotionImage {
onSend?: (e?: Emotion) => void
private dataSource: EmotionDataSource = new EmotionDataSource()
async aboutToAppear(): Promise<void> {
this.dataSource.initList(EmotionList)
}
build() {
Grid() {
GridItem() {
Text('所有表情').fontSize(13).fontColor('#666').width('100%')
}
.columnStart(0)
.columnEnd(this.columnCount() - 1)
LazyForEach(this.dataSource, (e: Emotion) => {
GridItem() {
Column({ space: 4 }) {
Image(e.url).width('100%').aspectRatio(1)
Text(e.name).fontSize(9).fontColor('#999').maxLines(1)
}
.width('100%')
.alignItems(HorizontalAlign.Center)
}
.onClick(() => {
this.onSend?.(e)
})
})
}
.edgeEffect(EdgeEffect.Spring, { alwaysEnabled: true })
.cachedCount(4)
.width('100%')
.height('100%')
.rowsGap(5)
.columnsGap(5)
.columnsTemplate('1fr '.repeat(this.columnCount()))
.padding({
top: 10,
left: 10,
right: 10
})
.maxCount(this.dataSource.totalCount() + 1)
}
columnCount(): number {
return 5
}
}4、完成emoji+emotion图片结合
收起
自动换行
深色代码主题
复制
/**
* @fileName : EmotionImageView.ets
* @author : @cxy
* @date : 2025/12/19
* @description : 表情组件
*/
import EmojiView from './EmojiView'
import { Emotion, EmotionImage } from './EmotionImage'
@Component
export struct EmotionComponent {
@Link enableSend: boolean
onSelectEmoji?: (value: string) => void
onDeleteEmoji?: () => void
onSendEmotion?: (e?: Emotion) => void
@State selectedIndex: number = 0
@Prop emotionHeight: number = 310
build() {
Column() {
Row({ space: 8 }) {
Image($r('app.media.chatbar_emoji'))
.width(34)
.height(34)
.borderRadius(10)
.backgroundColor(this.selectedIndex === 1 ? '#fff' : '#eee')
.padding(6)
.onClick(() => {
this.selectedIndex = 0
})
Image($r('app.media.startIcon'))
.width(34)
.height(34)
.borderRadius(10)
.padding(5)
.backgroundColor(this.selectedIndex === 0 ? '#fff' : '#eee')
.onClick(() => {
this.selectedIndex = 1
})
}
.justifyContent(FlexAlign.Start)
.alignItems(VerticalAlign.Center)
.width('100%')
.padding({ left: 15, top: 5, bottom: 5 })
.backgroundColor('#fff')
Swiper() {
EmojiView({
enableDel: this.enableSend,
onSelect: this.onSelectEmoji,
onDelete: this.onDeleteEmoji
})
EmotionImage({
onSend: this.onSendEmotion
})
}
.index(this.selectedIndex)
.layoutWeight(1)
.width('100%')
.indicator(false)
.loop(false)
.backgroundColor('#efefef')
.onChange((index: number) => {
this.selectedIndex = index
})
}
.alignItems(HorizontalAlign.Start)
.justifyContent(FlexAlign.Start)
.width('100%')
.height(this.emotionHeight)
}
}完整的demo
展开全部 
1楼编辑于2025-12-19 11:17 来自四川
写回答
所见即所得编辑器, editor1
工具栏 撤消 快捷键 Ctrl+Z 重做 快捷键 Ctrl+Y 格式刷 快捷键 Ctrl+Shift+C 清除格式 加粗 快捷键 Ctrl+B 倾斜 快捷键 Ctrl+I 下划线 快捷键 Ctrl+U 删除线格式调整字号 文本颜色 背景颜色 增加缩进量 减少缩进量 左对齐 居中 右对齐 两端对齐段落间距行间距 项目列表 编号列表 插入水平线 块引用 插入/编辑超链接 快捷键 Ctrl+K 取消超链接 图片 表格 上传附件 导入Word 插入代码 MD编辑器 全屏模式
0/50000
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
相关推荐
换一批
1
HarmonyOS 正则表达式怎么匹配emoji表情\ud83d\ude06(API12+)
2
HarmonyOS string是否包含表情包 (API12+)
3
HarmonyOS TextInput输入框如何正则过滤emoji表情符号?(API15+)
4
HarmonyOS 如何实现表情文本转图片显示在RichEditor中?(API12+)
5
HarmonyOS 如何基于@ohos.graphics.text文本模块绘制emoji(API12+)
6
HarmonyOS 如何实现圆形头像(API12+)
7
HarmonyOS 人脸检测功能如何实现 (API12+)
8
HarmonyOS 如何实现图片上编辑文字的功能(API12+)
了解社区公约,与您携手共创和谐专业的开发者社区。




































苏公安网备 32011402010933号