使用onDraw,我想創建一個自定義文本視圖,根據文本值更改顏色。例如,如果文本值是「你好」,我希望它是紅色的,如果它說「再見」,我希望它是綠色的。任何幫助不勝感激。根據文本更改TextView顏色
回答
我想通過使用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);
}
}
您可以覆蓋setText()並使用setTextColor()設置顏色。
你也可以在onDraw中做它,但它不值得體重,因爲它可能會在onDraw中傳遞很多次。
使用該工具來獲取文本:
TextView text = (TextView)findViewById(R.id.textid);
String value = text.getText().toString();
然後檢查文本是什麼,改變顏色:
if (value.equals("hello")) {
text.setBackgroundColor(yourcolor);
}
您可以實現TextWatcher和使用onTextChanged()
更多一點這裏在Android Docs
我是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()
提取代碼並更改yourEditText
到this
,或將其放置在構造函數代替:
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
}
我需要做的這onDraw有我在我的問題表達清楚,因爲我需要這個TextView的到是一個自定義的listview行項目。 – AndroidDev
那麼,你不需要一個自定義的'TextView',但是你可以使用完全相同的代碼,只需在構造函數中將這個'TextWatcher'附加到'TextView'。 – Eric
@ DroidDevAge13我編輯了我的帖子,試圖解釋我在上面得到的。希望這已經足夠清楚了;;) – Eric
- 1. 根據值更改文本顏色
- 2. 根據圖像更改文本顏色
- 3. 檢查並更改TextView文本顏色
- 4. android TextView:單擊更改文本顏色
- 5. 根據文本狀態更改TextView中某段文本的顏色
- 6. 更改TextView顏色
- 7. 根據值更改顏色
- 8. 根據值更改顏色
- 9. Android TextWatcher根據文本更改EditText文本顏色。多個EditText
- 10. 根據文本更改UITableViewCell中文本的顏色
- 11. 根據背景色反轉TextView的文本顏色?
- 12. 更改TextView的顏色
- 13. 根據位置或標誌更改顏色textview
- 14. 如何根據Access數據庫值更改文本顏色Asp.net
- 15. Textview不會改變文本顏色
- 16. 根據背景圖像的顏色改變文本顏色
- 17. 文本切換器更新textview顏色
- 18. 根據控件的背景顏色更改控件的文本顏色
- 19. 根據動態設置的背景顏色更改文本顏色的值
- 20. 根據它背後的顏色動態更改文本顏色(谷歌地圖)
- 21. 如何使用javascript根據背景顏色更改文本顏色?
- 22. Python:根據Var值更改文本顏色
- 23. 我需要根據文本更改Div Eliment的顏色
- 24. 如何根據ErrorMessage()方法更改Gridview的文本顏色?
- 25. 如何根據$ value更改文本的顏色
- 26. 根據條件是否反應更改文本顏色
- 27. JavaScript - 根據正則表達式更改文本的顏色
- 28. 根據標題文本更改列的背景顏色
- 29. 根據值更改文本框背景顏色
- 30. DJANGO - 根據值更改文本顏色(HTML&JS)
改變你的if語句,它不會工作 – ceptno
剛剛看到它,我的壞:) – Lotzki