Android防微信表情功能

上图

Android防微信表情功能_第1张图片

使用:

1、在et_input输入框中显示表情,可自由删除

@BindView(R.id.emoji_vp)
    lateinit var emoji_vp: ViewPager
    @BindView(R.id.emoji_dv)
    lateinit var emoji_dv: DotView
    private var mVpEmoAdaper: EmoViewPagerAdaper? = null


/**
* 其他处理...
*/

/**
     * 初始化表情
     */
    private fun initEmoji()
    {
        emoji_dv.initData(8, 0)
        mVpEmoAdaper = EmoViewPagerAdaper()
        emoji_vp.adapter = mVpEmoAdaper
        emoji_vp.setOnPageChangeListener(this)
        mVpEmoAdaper!!.onEmoItemClickListener = this
    }
    override fun onPageSelected(position: Int) {
        //当ViewPage页面切换时,设置下方小圆点的显示
        emoji_dv.changeCurrentPage(position)
    }
    override fun onPageScrollStateChanged(state: Int) {
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }
    override fun onEmoItemClick(emoResId: Int, emoName: String?) {
        var emoName = "$emoName"
        //创建可以图文混排的类
        val ssb = SpannableString(emoName)
        //得到图片
        val drawable = resources.getDrawable(emoResId)
        val size = SizeUtils.dp2px(30f)
        drawable.setBounds(0, 0, size, size)
        //创建ImageSpan
        val `is` = ImageSpan(drawable)
        //对文字进行图片替换(Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 表示删除时一个个删)
        ssb.setSpan(`is`, 0, emoName.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        //得到当前输入框中的内容
        val originalContent = et_input.text
        //得到当前输入框中的光标位置
        val selectionEnd = et_input.selectionEnd

        //在光标处插入内容
        originalContent.insert(selectionEnd, ssb)
    }

    override fun onDelItemClick() {
        //因为不知道具体表情文字内容的长度,所以模拟系统的删除键功能
        et_input.dispatchKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
        et_input.dispatchKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL))
    }

2、在列表中展示表情

val ssb = EmoParser.parseContent(activity, et_input.text.toString())
tv_msg.text = ssb

代码https://download.csdn.net/download/qq_26075861/11635874

你可能感兴趣的:(android,Kotlin)