Android开发表情emoji功能开发
用图片做的表情,但能在TextView中显示正常
一、思路:
图片的名称和表情字符对应起来
二、效果图:
看视频直观点:
[video(video-r5PpWilG-1728554762399)(type-bilibili)(url-https://player.bilibili.com/player.html?aid=958138498)(image-https://img-blog.csdnimg.cn/img_convert/f2be5a5f5f456177a7ed05fed7e6bdb5.jpeg)(title-Android开发教程实战案例源码分享-emoji功能开发)]
三、关键代码:
class MainActivity : AppCompatActivity() {
private lateinit var rvEmoji: RecyclerView
private lateinit var etContent: AppCompatEditText
private lateinit var tvContent: TextView
private lateinit var tvSubmit:TextView
private lateinit var mContext:Context
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mContext = this
AndroidEmoji.init(mContext)
RongUtils.init(mContext)
rvEmoji = findViewById(R.id.rv_emoji)
etContent = findViewById(R.id.et_content)
tvContent = findViewById(R.id.tv_content)
tvSubmit = findViewById(R.id.tv_submit)
rvEmoji.run {
itemAnimator = null
layoutManager = GridLayoutManager(mContext,7, RecyclerView.VERTICAL,false)
addItemDecoration(object : RecyclerView.ItemDecoration(){
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
outRect.top = dpToPx(4)
outRect.bottom = dpToPx(4)
}
})
adapter = EmojiAdapter{ emojiStr ->
if(TextUtils.isEmpty(emojiStr)){
etContent.onKeyDown(
KeyEvent.KEYCODE_DEL,
KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)
)
}else{
etContent.selectionStart.let { index ->
val resultStr: String = AndroidEmoji.replaceEmojiWithText(emojiStr.toString())
if(index < 0 || index >= etContent.editableText.length){
etContent.append(AndroidEmoji.ensure(resultStr))
}else{
etContent.editableText.insert(index,AndroidEmoji.ensure(resultStr))
}
}
}
}.apply { setNewInstance( AndroidEmoji.getEmojiList() ) }
}
tvSubmit.setOnClickListener{
// 无文本拦截
if(etContent.text?.isEmpty() != false || etContent.text?.trim()?.isEmpty() != false){
show(R.string.mood_publish_tip_1)
} else {
tvContent.run {
text = TextViewUtils.getSpannable( etContent.text.toString()){ spannable ->
runOnUiThread {
text = spannable
}
}
}
}
}
}
}
class EmojiAdapter(val callback :(CharSequence) -> Unit) : BaseQuickAdapter<AndroidEmoji.EmojiInfo, BaseViewHolder>(R.layout.item_emoji) {
override fun convert(holder: BaseViewHolder, item: AndroidEmoji.EmojiInfo) {
holder.getView<AppCompatImageView>(R.id.iv_emoji).run{
if(holder.bindingAdapterPosition == data.size - 1){
setImageResource(R.mipmap.rc_icon_emoji_delete)
}else{
setImageDrawable(context.getDrawables(item.resId))
}
setOnClickListener{
if(holder.bindingAdapterPosition != data.size - 1){
val chars = Character.toChars(item.code)
val key = StringBuilder(chars[0].toString())
for (i in 1 until chars.size) { key.append(chars[i]) }
callback(key)
}else{
callback("")
}
}
}
}
}
四、项目demo源码结构图:
有问题或者需要完整源码的私信我