使用 onDraw,我想创建一个自定义文本视图,根据其文本值更改颜色。例如,如果文本值为“hello”,我希望它是红色的,如果它说“bye”,我希望它是绿色的。非常感谢任何帮助。
问问题
1241 次
5 回答
2
我不一定确定你为什么要在onDraw()
. 除非您有充分的理由设置自定义TextView
/ EditText
,否则没有必要。
为了简化您的情况,您可以实现 aTextWatcher
来执行此操作,并且在 中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
}
于 2012-12-06T18:50:38.330 回答
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);
}
}
于 2012-12-06T20:40:18.450 回答
0
您可以覆盖 setText() 并使用 setTextColor() 设置颜色。
您也可以在 onDraw 中执行此操作,但这不值得,因为它可能会在 onDraw 中多次传递。
于 2012-12-06T18:37:28.117 回答
0
使用它来获取文本:
TextView text = (TextView)findViewById(R.id.textid);
String value = text.getText().toString();
然后检查文本是什么并更改颜色:
if (value.equals("hello")) {
text.setBackgroundColor(yourcolor);
}
于 2012-12-06T18:38:11.203 回答
0
您可以实现 TextWatcher 并使用onTextChanged()
于 2012-12-06T18:40:33.387 回答