2012-02-17 41 views
3

我想動態改變EditText中的顏色。 具體而言:如何在EditText中輸入彩色文字

後我按下按鈕,輸入到EditText文本我變爲紅色;(在的EditText已經文本保持其原始顏色)

後餘按在按鈕再次,輸入到文本EditText我變成其他顏色,另外,文字的顏色,這已經在EditText遺體。

+0

爲該按鈕設置一個'onClickListener',同時可能還有一個名爲'isPressed'的布爾值。如果'isPressed'爲false,則使用@ Sunil的答案並將'isPressed'設置爲true。這樣,當用戶再次按下它時,您可以檢查「isPressed」,因爲它是真的,請將其設置爲false,並將EditText的顏色恢復爲原始。 – Mxyk 2012-02-17 13:23:28

+0

我想要的效果是一種編輯文本中不同種類的顏色,例如:首先我輸入「abc」,它們是黑色的,現在我按下按鈕,繼續輸入「def」,我想要「abc」黑色但「def」變爲紅色,然後再次按下,輸入「123」,「abc」爲黑色,「def」爲紅色,「123」變爲其他顏色! – user1138902 2012-02-17 13:47:15

回答

0

可以能夠將顏色應用到你的EditText像:

yourEdiText.setTextColor(0xff0000ff); 

和特定情況下,你必須把這些代碼。

享受。 :)

7

您也可以使用下面的代碼來設置你的編輯文本的文本顏色

EditText et = (EditText) findViewById(R.id.edit1); 
// to set text color using RGB code 
et.setTextColor(Color.parseColor("#00ff00")); 
+2

除了這個答案,使用[這個鏈接](http://immigration-usa.com/html_colors.html)來找到你想要的顏色(用這個答案中的'#'符號替換6個字符與6你想要的顏色)。 – Mxyk 2012-02-17 13:19:23

+0

你可以使用http://html-color-codes.info/也選擇你的顏色,並得到其rgb代碼 – 2012-02-17 13:24:53

+0

+1邁克,良好的迴應 – 2012-02-17 13:25:17

0

我想出一個簡單的解決方案通過使用標籤,如HTML

ImageBtn.setOnClickListener(new OnClickListener() {  
    @override 
    public void onClick(View view) { 
    // TODO Auto-generated method stub 
     mContentEdit.append("[1;34m [m"); //this is the tag I use 
     //here I simplely append new tag to the end ,we can also add this tag where cursor is 
    } 
}); 

添加TextWatcher爲我的EditText;

mContentEdit.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 
      switch(mInsertIndex){ 
      case 1: 
       mSelection = mContentEdit.getSelectionStart(); 
       break; 
      case 2: 
       mContentEdit.setSelection(mSelection); 
       break; //recover the cursor's position 
      } 
      } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 
      switch (mInsertIndex) { 
      case 1: 
       //this is p:Pattern p = Pattern.compile("\\[[\\d{1};]*(3\\d{1})m(.*?)\\[[\\d;]*m"); 
       Matcher m = p.matcher(s.toString()); 
       StringBuffer sb = new StringBuffer(); 
       while(m.find()){ 
        m.appendReplacement(sb, "<font color=#"+CssUtil.getColor(m.group(1))+">"+"[1;"+m.group(1)+"m"+m.group(2)+"[m"+"</font>"); 
       } //add a tag otherwise the next time we input,color will be gone; 
       m.appendTail(sb); 
       mInsertIndex = 2; 
       mContentEdit.setText(Html.fromHtml(sb.toString())); 
       break; 
            //replace tag by html color 
      case 2: 
       mInsertIndex = 1; 
       break; 
            //avoid recursive message loop 
      } 

     } 

此代碼爲我的應用程序做得很好。我可以將edittext中的原始文本直接發佈到網站,並且網站將解析css顏色的標籤;

但仍有缺陷:

1:如果我不希望看到的標籤? 在我的問題中,我需要的實際上是一個顏色狀態機,一旦按下btn,只要你輸入了,顏色就會生效。但在我的解決方案中,我必須輸入標籤。

2:隨着輸入長度的增長,效率可能是一個問題;如你所見,正則表達式被使用;

所以,仍然期待英雄解決我的問題!

3

使用此代碼:

Spannable spannable = youreditText.getText(); 
spannable .setSpan(new BackgroundColorSpan(Color.argb(a, r, g, b)), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

如果你不想改變顏色的插入的文本,但你想有一個文本beetween簡單有色索引使用:

Spannable spannable = youreditText.getText(); 
spannable .setSpan(new BackgroundColorSpan(Color.argb(a, r, g, b)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

希望這有助於!