输入区域
自定义输入框按钮UI
从 1.5.1 版本开始,IMKit 支持配置会话页面输入框的 UI 组件。可通过 setInputAreaComponentConfig 接口设置自定义组件配置。
接口定义
TypeScript
setInputAreaComponentConfig(componentConfig: InputAreaComponentConfig): void;
参数说明
InputAreaComponentConfig
| 参数名 | 类型 | 必填 | 说明 | 支持版本 |
|---|---|---|---|---|
identifier | ComponentIdentifier | 是 | 组件标识,指定要自定义的组件类型。详见下方"支持的组件类型" | 1.5.1 |
component | WrappedBuilder<[InputAreaComponentData]> | 是 | 自定义组件构建器,用于构建自定义 UI 组件 | 1.6.0 |
InputAreaComponentData
| 参数名 | 类型 | 说明 | 支持版本 |
|---|---|---|---|
context | Context | 应用上下文对象 | 1.6.0 |
convId | ConversationIdentifier | 会话标识对象 | 1.6.0 |
data | string | 透传参数。当 identifier 为 InputBarVoiceButton 时,值为 "Voice"(语音类型)或 "Text"(文本类型);当 identifier 为 InputBarEmoticonButton 时,值为 "Emoticon"(表情类型)或 "Text"(文本类型);当 identifier 为 DestructBarVoiceButton 时,值为 "Voice"(语音类型)或 "Text"(文本类型) | 1.6.0 |
支持的组件类型
identifier 支持的组件类型说明:
| 组件类型 | 说明 |
|---|---|
InputBarVoiceButton | 输入框左侧语音按钮 |
InputBarEmoticonButton | 输入框表情按钮 |
InputBarSendButton | 输入框发送按钮 |
InputBarPluginButton | 输入框插件加号按钮 |
InputBarExpandTextAreaButton | 文本输入组件输入了大于 2 行文本时,输入框左侧的展开输入按钮 |
EmoticonBoardAddButton | 表情面板左下角的添加按钮 |
示例代码
TypeScript
// 设置接口
let InputBarPluginButton: InputAreaComponentConfig = {
identifier: ComponentIdentifier.InputBarPluginButton,
component: wrapBuilder(buildCustomInputBarPluginView),
}
RongIM.getInstance().conversationService().setInputAreaComponentConfig(InputBarPluginButton)
// 组件
@Builder
export function buildCustomInputBarPluginView(componentData: InputAreaComponentData) {
CustomInputBarPluginView({ context: componentData.context, convId: componentData.convId, type: componentData.data })
}
@Component
export struct CustomInputBarPluginView {
@Prop context: Context;
@Prop convId: ConversationIdentifier;
@Prop type: string;
aboutToAppear(): void {
console.log("CustomInputBarPluginView aboutToAppear " + this.type)
}
build() {
Image($r('app.media.default_fmessage'))
.size({ width: 30, height: 30 })
.onTouch(() => {
promptAction.showToast({ message: `点击了按钮` })
})
}
}
增加 + 号扩展栏插件
内置插件说明
IMKit 内置插件列表如下所示。其中位置插件 SDK 仅定义,实际需要 App 侧来实现。
| 插件 | 插件名称 | 插件名称常量 |
|---|---|---|
| 图片插件 | ImagePlugin | ImagePluginName = "RC:ImagePlugin" |
| 小视频插件 | CameraPlugin | CameraPluginName = "RC:CameraPlugin" |
| 文件插件 | FilePlugin | FilePluginName = "RC:FilePlugin" |
| 阅后即焚插件 | DestructPlugin | DestructPluginName = "RC:DestructPlugin" |
| 位置插件 | LocationPlugin | LocationPluginName = "RC:LocationPlugin" |
实现自定义插件
示例代码
TypeScript
import { IBoardPlugin } from '@rongcloud/imkit';
import { ConversationIdentifier } from '@rongcloud/imlib';
import { promptAction } from '@kit.ArkUI';
/**
* 自定义插件
*/
export class CustomInfoMessagePlugin implements IBoardPlugin {
/**
* 插件名,用来判断插件唯一标识
*/
pluginName(): string {
return "CustomInfoMessagePlugin"
}
/**
* 返回插件的标题
*/
obtainTitle(context: Context): string | Resource {
return "自定义小 灰条消息"
}
/**
* 返回插件的图标
*/
obtainImage(context: Context): Resource {
return $r("app.media.startIcon");
}
/**
* 插件的点击事件
*/
onClick(context: Context, conId: ConversationIdentifier): void {
if (!conId) {
return;
}
// 处理具体的点击事件
// 开发者按照实际情况处理
promptAction.showToast({ message: '点击了自定义插件' })
}
/**
* 是否在会话中显示该插件
*/
onFilter(conId: ConversationIdentifier): boolean {
return true;
}
}
自定义插件UI
从 1.5.1 版本开始,IMKit 默认允许配置插件的UI组件。可以根据插件名替换自定义插件的UI,如果想替换SDK内置的插件UI,插件名参照内置插件说明。
TypeScript
@Builder
export function buildCustomMessagePluginView(context: Context, convId: ConversationIdentifier) {
CustomMessagePluginView({ context: context, convId: convId })
}
@Component
export struct CustomMessagePluginView {
@Prop context: Context;
@Prop convId: ConversationIdentifier;
build() {
Column() {
Text("自定义插件名").width("100%").fontSize(12).textAlign(TextAlign.Center)
Image($r("app.media.rc_file_apk_icon")).width(40).height(40)
}.width("100%").height("100%")
.justifyContent(FlexAlign.SpaceEvenly).alignItems(HorizontalAlign.Center)
}
}
// 替换自定义插件的UI组件
RongIM.getInstance().conversationService().setBoardPluginView("CustomInfoMessagePlugin", wrapBuilder(buildCustomMessagePluginView))
将插件放到扩展栏里面
需要在聊天页面出现前调用
添加插件
TypeScript
let customInfoMessagePlugin = new CustomInfoMessagePlugin();
RongIM.getInstance().conversationService().addBoardPlugin(customInfoMessagePlugin);