一个可定制性优良支持@的输入框组件。支持定制响应字符,自定义高亮Span,输入与数据及时同步。
在module下的build.gradle文件中添加如下依赖
implementation 'studio.kio:MentionLibrary:1.0.0'
<studio.kio.mentionlibrary.MentionEditText
android:id="@+id/et_sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
val myMentionEditText = MentionEditText(context)
data class User(
val Id: Int,
val name: String
)
MentionUtil.withType(User::class)//返回MentionHandlerBuilder以支持mention事件及定制
.attach(et_sample)//返回MentionEditTextHandler以支持插入及获取已关联的mentionItems
mentionHandlerBuilder.tag('@')//设置响应的字符
.onMention(object : OnMentionInsertedListener<User> {
override fun onMentionInserted(position: Int) {
//用户输入了@时这里会被回调
}
})
.decorate(object : MentionHandlerBuilder.MentionDecorator {
override fun getSpan(): Any {
//自定义高亮样式
return ForegroundColorSpan((0xff000000 or (Math.random() * Int.MAX_VALUE).toLong()).toInt())
}
})
mentionHandler.insert(user, label, position)
mentionHandler.getMentionItems()//List<MentionItem>
this.mentionHandler = MentionUtil.withType(User::class)
.onMention(object : OnMentionInsertedListener<User> {
override fun onMentionInserted(position: Int) {
mentionHandler.insert(user, user.name, position)
}
})
.decorate(object : MentionHandlerBuilder.MentionDecorator {
override fun getSpan(): Any {
return ForegroundColorSpan((0xff000000 or (Math.random() * Int.MAX_VALUE).toLong()).toInt())
}
})
.tag('@')
.attach(et_sample)
//触发@
mentionHandler.automaticallyAppend()
//获取关联的用户
mentionHandler.getMentionItems()