2016-02-08 27 views
5

Sto lavorando a un'applicazione in cui l'utente può pubblicare contenuti in un feed. Nel mio testo di modifica (usato per comporre) il colore del testo è grigio. Tuttavia, quando l'utente digita un tag hash, ad es. #help Ho bisogno di colorare il testo in nero durante la digitazione, quindi quando l'utente digita "#" il testo deve essere colorato in nero fino a quando non inizia una nuova parola, quindi il colore del testo deve tornare in grigio.Modifica il colore del testo mentre l'utente digita i tag hash

Ho provato a usare un osservatore di testo e una stringa da colorare.

Ecco quello che ho fatto con la textwatcher onTextChanged

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


      //Remove Text watcher to prevent infinite look 
      mTextField.removeTextChangedListener(contentFieldWatcher); 
      String startChar = null; 
       startChar = Character.toString(s.charAt(start)); 
       L.i(getClass().getSimpleName(), "CHARACTER OF NEW WORD: " + startChar); 


    if (startChar.equals("#")) { 
        tagCheck(s.toString().substring(start), start, start + count); 
       } 
      } 

metodo tagCheck

private void tagCheck(String s, int start, int end) { 
     mSpannable.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.black_colour)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
    } 

mSpannable è uno Spannable.

Il problema con questo metodo è che "#" viene visualizzato come startChar, tuttavia quando l'utente digita il carattere successivo, sia esso simbolo o lettera, viene visualizzato come startChar. Dove come se l'utente digitasse santa - 's' rimane startChar. Quindi il problema che sto affrontando è come colorare dinamicamente il testo mentre l'utente digita un hashtag.

Le lettere così semplici funzionano correttamente, tuttavia quando si utilizza un simbolo non lo fa. Spero che la domanda abbia chiarezza .. ho guardato questo per alcuni giorni ed è tutto diventato confuso :)

risposta

3

ho provato e trovato il soloution

È possibile utilizzare seguente codice

Spannable mspanable; 
int hashTagIsComing = 0; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 



    final EditText edt = (EditText) findViewById(R.id.editText1); 

    mspanable = edt.getText(); 

    edt.addTextChangedListener(new TextWatcher() { 

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

      String startChar = null; 

      try{ 
       startChar = Character.toString(s.charAt(start)); 
       Log.i(getClass().getSimpleName(), "CHARACTER OF NEW WORD: " + startChar); 
      } 
      catch(Exception ex){ 
       startChar = " "; 
      } 

       if (startChar.equals("#")) { 
        tagCheck(s.toString().substring(start), start, start + count); 
        hashTagIsComing++; 
       } 

       if(startChar.equals(" ")){ 
        hashTagIsComing = 0; 
       } 

       if(hashTagIsComing != 0) { 
        tagCheck(s.toString().substring(start), start, start + count); 
        hashTagIsComing++; 
       } 
     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

     } 
    }); 



} 


private void tagCheck(String s, int start, int end) { 
    mspanable.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
} 
+0

Si noti che la maggior parte fanno hashtag chiaro da personaggi inapropriate –

+0

c'è un problema con auto suggerire, se il l'utente fa clic su un suggerimento automatico, perdiamo la colorazione –

1

Sono riuscito a ottenere il comportamento che stai descrivendo, è solo che sto impostando il testo colorato su un TextView separato. Vedi lo screenshot e il codice qui sotto. Spero che questo aiuta

In afterTextChanged di chi ascolta:

@Override 
public void afterTextChanged(Editable s) { 
    editText.removeTextChangedListener(textWatcher); 
    colorText(s.toString()); 
} 

metodo per trovare i caratteri "#" e il testo di colore, fino primo spazio:

private void colorText(String s) { 
    if (!TextUtils.isEmpty(s)) { 
     Spannable spannable = new SpannableString(s); 
     int position = 0; 
     position = s.indexOf("#", position); 
     while (position != -1) { 
      colorSpannable(spannable, position, s.indexOf(" ", position + 1) != -1 ? s.indexOf(" ", position + 1) : s.length()); 
      position = s.indexOf("#", position + 1); 
     } 
     textView.setText(spannable); 
    } 
    editText.addTextChangedListener(textWatcher); 
} 

colorSpannable aggiunge semplicemente il colore dall'inizio indice al fine indice

private void colorSpannable(Spannable s, int start, int end){ 
    s.setSpan(new ForegroundColorSpan(Color.BLUE), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
} 

enter image description here