[英]Change TextView Color Depending On Text
我想使用onDraw创建一个自定义文本视图,该视图根据其文本值更改颜色。 例如,如果文本值为“ hello”,我希望它是红色,如果它说“再见”,我希望它是绿色。 任何帮助,不胜感激。
我不确定您为什么要在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以更具创造性的方式进行操作。
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文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.