を入力中には、私はedittext
、その中のユーザーの種類@メンションと#ハッシュの色が変化しなければならないときがあります。私はtextwatcher
を使用しましたが、それは遅く、入力中にいくつかの文字が欠けています。私は選択肢について聞きたいです。のAndroidのEditText色の変化
これは私のコードです:
var previousString = ""
override fun initTextAreaView() {
if(twitterAvailable && twitterIsEnabled) CHARLIMIT = 280
if(!twitterIsEnabled && linkedInAvailable && linkedInIsEnabled) CHARLIMIT = 700
charcount.text = CHARLIMIT.toString()
content.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if(s.toString()!=previousString)checkMentionsAndHashtags(s.toString())
}
})
}
var textSpanList: ArrayList<TextSpan> = ArrayList()
private fun checkMentionsAndHashtags(s: String) {
previousString = s
textSpanList = ArrayList()
val words = s.split(" ")
for(word in words){
if(word.length>0) {
if (word[0] == '@' || word[0] == '#') {
val n = s.lastIndexOf(word)
textSpanList.add(TextSpan(n, (n+word.length)))
}
}
}
changeTextColor(s, SpannableStringBuilder(s))
}
private fun changeTextColor(s: String = "", str: SpannableStringBuilder = SpannableStringBuilder("")) {
Log.d("TEXTSPANS", textSpanList.toString())
if(textSpanList.size>0){
for(span in textSpanList) {
str.setSpan(ForegroundColorSpan(resources.getColor(R.color.colorPrimary)), span.startPos, span.endPos, 0)
}
}
val cursorPos = content.selectionEnd
content.text = str
content.setSelection(cursorPos)
}
のように試してみて、私は1 [ライブラリ](https://github.com/tylersuehr7/social-text-viewました)これはあなたを助けるかもしれない –
@HarshadPrajapatiそれはedittextとの仕事をしなかった。カーソル位置も扱っているので、edittextで作業する必要があります。提案ありがとう:) – Deepan
私の更新された記事を参照してください..それはあなたに役立つ..その作品は私にうまく.. – Raja