2017-05-23 45 views
1

我有一個編輯文本時編輯的文本中鍵入內容,如果這個詞與#特定單詞的顏色應該改變開始,一個字的Android EditText上改變顏色,同時輸入(動態)

我已經實現textwatcher和發現,如果文字以#開頭,但不知道如何更新動態色彩,

試過SpannableStringBuilder ssb = new SpannableStringBuilder(yourText)但其靜態的,任何人都可以幫我要動態實現

這裏是我的C頌

myEditTxt.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence text, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence text, int start, int before, int count) { 

      if (text.charAt(start) == '#') { 
       //here i needs to update the typing text color 
      } 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 
+0

你檢查這https://stackoverflow.com/questions/22291644/how-to-change-color-of-words-帶標籤 –

+0

遲到的答案,但也檢查這可能會幫助你:https://stackoverflow.com/a/45658878/6021469 –

回答

0

終於找到了回答並按預期工作。

private int intCount = 0, initialStringLength = 0; 
private String strText = ""; 

上的文字改變

@Override 
     public void onTextChanged(CharSequence text, int start, int before, int count) { 

      String strET = editText.getText().toString(); 
      String[] str = strET.split(" "); 
      int cnt = 0; 
      if (text.length() != initialStringLength && text.length() != 0) { 
       if (!strET.substring(strET.length() - 1).equals(" ")) { 
        initialStringLength = text.length(); 
        cnt = intCount; 
        for (int i = 0; i < str.length; i++) 
         if (str[i].charAt(0) == '#') 
          strText = strText + " " + "<font color='#EE0000'>" + str[i] + "</font>"; 
         else 
          strText = strText + " " + str[i]; 
       } 
       if (intCount == cnt) { 
        intCount = str.length; 
        editText.setText(Html.fromHtml(strText)); 
        editText.setSelection(textShareMemories.getText().toString().length()); 
       } 
      } else { 
       strText = ""; 
      } 

     } 
0

也許這樣的事情

 @Override 
     public void onTextChanged(CharSequence text, int start, int before, int count) { 

     if (text.charAt(start) == '#') { 

      SpannableString spannableString = new SpannableString(editText.getText().toString()); 
      ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.yourColor)); 
      spannableString.setSpan(foregroundSpan, start, 
      spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      editText.setText(spannableString); 

     } 
    } 
+0

我試過了,它改變了完整的文本顏色,但它應該只改變特定的文字顏色 – John

+0

檢查我的編輯,它應該工作。只是修改你的單詞的結尾,而不是完整的長度 – tompadre

0

如果你只是想改變 「#」 字符。你可以試試:

int index = myEditTxt.getText().toString().indexOf("#"); 
if (index != -1) { 
    SpannableString spannable = new  
    SpannableString(myEditTxt.getText().toString()); 
    ForegroundColorSpan fcs = new ForegroundColorSpan(color); 
    // Set the text color 
    spannable.setSpan(fcs, index, index + 1, 
     Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

    mEditText.setText(spannable); 
}  

如果你只想在「#」之後和之前改變。你可以嘗試在你的聽衆:

if (text.charAt(start) == '#') { 
    SpannableString spannable = new  
    SpannableString(myEditTxt.getText().toString()); 
    ForegroundColorSpan fcs = new ForegroundColorSpan(color); 
    // Set the text color 
    if (start > 0) 
     spannable.setSpan(fcs, 0, start -1, 
     Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
    if (start < myEditTxt.getText().toString().length()) 
     spannable.setSpan(fcs, start + 1, myEditTxt.getText().toString().length(), 
     Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

    mEditText.setText(spannable); 
} 
+0

它會完全改變顏色,但我想改變只有一個世界# – John

+0

對不起,我編輯了我的答案 – anthorlop

+0

設置相同的文本在同一個edittext視圖將掛視圖和得到ANR。我用HTML選項更新了答案。謝謝您的幫助 – John

0

由於在這個環節How to change color of words with hashtags @Muhammed居內什建議

實現的,你可以根據它遵循

@Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 


     textView.removeTextChangedListener(this); 
     setTags(textView,s.toString()); 
     textView.setSelection(textView.getText().toString().length()); 
     textView.addTextChangedListener(this); 
    } 


private void setTags(TextView pTextView, String pTagString) { 
     SpannableString string = new SpannableString(pTagString); 

     int start = -1; 
     for (int i = 0; i < pTagString.length(); i++) { 
      if (pTagString.charAt(i) == '#') { 
       start = i; 
      } else if (pTagString.charAt(i) == ' ' || (i == pTagString.length() - 1 && start != -1)) { 
       if (start != -1) { 
        if (i == pTagString.length() - 1) { 
         i++; // case for if hash is last word and there is no 
         // space after word 
        } 

        final String tag = pTagString.substring(start, i); 
        string.setSpan(new ClickableSpan() { 

         @Override 
         public void onClick(View widget) { 
          Toast.makeText(MainActivity.this,"Click",Toast.LENGTH_LONG).show(); 
         } 

         @Override 
         public void updateDrawState(TextPaint ds) { 
          // link color 
          ds.setColor(Color.parseColor("#33b5e5")); 
          ds.setUnderlineText(false); 
         } 
        }, start, i, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        start = -1; 
       } 
      } 
     } 

     pTextView.setMovementMethod(LinkMovementMethod.getInstance()); 
     pTextView.setText(string); 
    } 

會增添色彩和點擊事件修改你也可以根據你的進一步要求修改它