2013-08-05 59 views
2

如果用戶鍵入一個字符串,並且它包含@我想要將文本的顏色更改爲紅色.i已嘗試使用textwatcher但得到堆棧溢出錯誤。我想更改顏色只有當@位於beginning.The代碼如下在Android編輯文本中爲特定文本設置文本顏色

topic.addTextChangedListener(new TextWatcher() { 

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

     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 

     } 

     public void afterTextChanged(Editable s) { 

      if (s.toString().matches("(@\\w+)")) { 
       topic.setText(Html.fromHtml(s.toString().replaceAll(
         "(@\\w+)", "<font color='#ffff0000'>$1</font>"))); 
      } 


     } 
    }); 
+0

您是否嘗試過同樣的邏輯之前,但與'onTextChanged',而不是'afterTextChanged' –

+0

@SakthiKumar相同的stackoverflow錯誤 – user1767260

回答

1
if (s.toString().matches("(@\\w+)")) 
{ 
     topic.setTextColor(Color.parseColor("#ffff0000")); 
} 
0

給出試試這個:

topic.addTextChangedListener(new TextWatcher() { 

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

    } 

    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 

    } 

    public void afterTextChanged(Editable s) { 

     if (s.toString().startsWith("@")) { 
      topic.setText(
       Html.fromHtml("<font color='#ffff0000'>"+ 
       s.toString().substring(1) 
       +"</font>")); 
     } 


    } 
}); 
0
if(s.toString().startsWith("@")) 
{ 
    topic.setTextColor(Color.RED); 
} 
1

你必須除去叔在設置新文本以編輯文本之前,請使用extwatcher。 試試這個代碼

  private TextWatcher textWatcher = new TextWatcher() { 

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

    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 

    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     editText.removeTextChangedListener(this); 
     String text = editText.getText().toString(); 

     if (text .matches("(@\\w+)")) { 
      editText.setText(Html.fromHtml(text .replaceAll(
        "(@\\w+)", "<font color='#ffff0000'>$1</font>"))); 
     } 

     editText.addTextChangedListener(this); 

    } 
}; 
0

追加在行 的結束,使光標和設置文本空附加

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

     } 

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


     } 

     @Override 
     public void afterTextChanged(Editable s) { 

      edt_customer_cc.removeTextChangedListener(this); 

      String text = edt_customer_cc.getText().toString(); 

      if (text .contains(";")) { 

       edt_customer_cc.setText(""); 

       // append to bring curser at end of line 
       edt_customer_cc.append(Html.fromHtml(text.replaceAll(
         "(\\;)", "<font color='#ff0000'>;</font>"))); 


      } 
      //editText.setText(text); 
      //editText.setSelection(text.length()); 
      edt_customer_cc.addTextChangedListener(this); 

     } 
    }); 
+0

它會好而光滑 –

相關問題