如何添加过滤器来编辑在群组聊天中提到用户的文本

2022-10-15 08:18:04标签androidtextwatcherandroid-textwatcher
提问

我如何在编辑文本中添加一个textwatcher,它可以检测字符“@”,并从一个与@的字符匹配的列表中建议用户名? 这就是我所尝试的,但有问题:

binding.etMessage.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(editable: Editable?) {
                if (editable?.contains("@") == true || editable?.contains("锛?quot;) == true
                ) {
                    val mNames = arrayOf("Amit","Sneha","Nishi")
                    val mArrayAdapter = context?.let {
                        ArrayAdapter(
                            it,
                            android.R.layout.simple_list_item_1,
                            android.R.id.text1,
                            mNames
                        )
                    }
                    mArrayAdapter?.filter?.filter(editable.substring())
                    binding.listMentionSuggestions.adapter = mArrayAdapter
                    binding.listMentionSuggestions.show()
                }
                else{
                    binding.listMentionSuggestions.hide()
                }
            }
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }
            override fun onTextChanged(charSequence: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })

▼版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说cuoshuo.com——程序员的报错记录

部分内容根据CC版权协议转载,如果您希望取消转载请发送邮件到cuoshuo8@163.com

辽ICP备19011660号-5