簡體   English   中英

在Android Java中鍵入時自動為Edittext中的Hashtag着色

[英]Automatic coloring of Hashtags in Edittext while typing in Android Java

因此,我的布局中有一個“ EditText”用於簡單的Textinput。 當我寫一個標簽時,例如“ #hello”,我希望該標簽以藍色突出顯示。

editText.setOnTextChangedListener(...) {
   public void afterTextChanged(...)
        // replace current String with editText.setText(Html.parse(...));
}

我已經嘗試了一些方法來實現這一目標-afterTextChangedListener中的SpannableStrings和基於HTML的setTexts-我遇到的唯一問題是,當我嘗試鍵入某些內容時,光標跳到了第一個位置-因此實時突出顯示不會我猜想在Android Java中不能這樣工作-至少我的算法不行。

有什么建議么?

正如您已經知道如何着色一樣,我只是建議您如何使用sunil sunny建議的方法來更改String並將光標保持在位置的技巧。

editText.setOnTextChangedListener(...) {
   public void afterTextChanged(...)
        int currentSelectionStart = editText.getSelectionStart()
        int currentSelectionEnd = editText.getSelectionEnd()
        // replace current String with Spannable for every "Hashtag"
        editText.setSelection(currentCursorPosition,currentSelectionEnd )
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM