2012-12-06 173 views
0

使用onDraw,我想創建一個自定義文本視圖,根據文本值更改顏色。例如,如果文本值是「你好」,我希望它是紅色的,如果它說「再見」,我希望它是綠色的。任何幫助不勝感激。根據文本更改TextView顏色

回答

1

我想通過使用onDraw的更有創意的方式來做到這一點。

public class MagnitudeTextView extends TextView { 

public MagnitudeTextView(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
} 

public MagnitudeTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // TODO Auto-generated constructor stub 
} 

public MagnitudeTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    // TODO Auto-generated constructor stub 
} 

/* 
* (non-Javadoc) 
* 
* @see android.widget.TextView#onDraw(android.graphics.Canvas) 
*/ 
@Override 
protected void onDraw(Canvas canvas) { 

    int height = getMeasuredHeight(); 
    int width = getMeasuredWidth(); 

    int px = width/2; 
    int py = height/2; 

    Paint Red = new Paint(Paint.ANTI_ALIAS_FLAG); 
    Red.setColor(Color.RED); 

    Paint White = new Paint(Paint.ANTI_ALIAS_FLAG); 
    White.setColor(Color.DKGRAY); 

    Paint Yellow = new Paint(Paint.ANTI_ALIAS_FLAG); 
    Yellow.setARGB(210, 105, 30, 0); 

    Paint Blue = new Paint(Paint.ANTI_ALIAS_FLAG); 
    Blue.setColor(Color.BLUE); 

    float textWidth = Red.measureText(String.valueOf(getText())); 

    String g = String.valueOf(getText()); 
    if (g.startsWith("3") || g.startsWith("4")) { 
     canvas.drawText(String.valueOf(getText()), px - textWidth/2, py, 
       White); 
    } 

    if (g.startsWith("6") || g.startsWith("5") || g.startsWith("7") 
      || g.startsWith("8")) { 
     canvas.drawText(String.valueOf(getText()), px - textWidth/2, py, 
       Yellow); 
    } 

    if (g.startsWith("9") || g.startsWith("10")) { 
     canvas.drawText(String.valueOf(getText()), px - textWidth/2, py, 
       Red); 
    } 
    // super.onDraw(canvas); 
} 

}

0

您可以覆蓋setText()並使用setTextColor()設置顏色。

你也可以在onDraw中做它,但它不值得體重,因爲它可能會在onDraw中傳遞很多次。

0

使用該工具來獲取文本:

TextView text = (TextView)findViewById(R.id.textid); 
String value = text.getText().toString(); 

然後檢查文本是什麼,改變顏色:

if (value.equals("hello")) { 

    text.setBackgroundColor(yourcolor); 
} 
+2

改變你的if語句,它不會工作 – ceptno

+0

剛剛看到它,我的壞:) – Lotzki

2

我是n不一定確定你爲什麼想在onDraw()中這樣做。除非您有充分理由設置自定義TextView/EditText,否則沒有必要。

爲了簡化您的情況,你可以實現一個TextWatcher要做到這一點,並在onTextChanged(),您可以通過使用.equals()比較字符串值設置顏色。

這裏是你的理論狀況的一個例子:

final EditText yourEditText = /* findViewById maybe? */; 
yourEditText.addTextChangedListener(new TextWatcher() { 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     if (s.equalsIgnoreCase("hello")) 
      yourEditText.setTextColor(Color.RED); 
     else if (s.equalsIgnoreCase("bye")) 
      yourEditText.setTextColor(Color.GREEN); 
     else // if it says neither "hello" nor "bye" 
      yourEditText.setTextColor(Color.BLACK); 
    } 

    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     // Nothing needs to happen here 
    } 

    public void afterTextChanged(Editable s) { 
     // Nothing needs to happen here 
    } 
}); 

如果你覺得它的必要onDraw()保持這一點,只需從onTextChanged()提取代碼並更改yourEditTextthis,或將其放置在構造函數代替:

public class YourTextView extends TextView { // Or extends EditText, doesn't matter 
    public YourTextView(Context context) { 
     this(context, null, 0); 
    } 

    public YourTextView(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public YourTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     addTextChangedListener(new TextWatcher() { 
      // Copy the TextWatcher code from the example above, replacing "yourEditText" with "YourTextView.this" 
     }); 
    } 

    // ... Rest of your class 
} 
+0

我需要做的這onDraw有我在我的問題表達清楚,因爲我需要這個TextView的到是一個自定義的listview行項目。 – AndroidDev

+0

那麼,你不需要一個自定義的'TextView',但是你可以使用完全相同的代碼,只需在構造函數中將這個'TextWatcher'附加到'TextView'。 – Eric

+1

@ DroidDevAge13我編輯了我的帖子,試圖解釋我在上面得到的。希望這已經足夠清楚了;;) – Eric

相關問題