根据文本更改TextView颜色

时间:2012-12-06 18:30:08

标签: java android textview ondraw

使用onDraw,我想创建一个自定义文本视图,根据文本值改变颜色。例如,如果文本值是“hello”,我希望它是红色的,如果它说“再见”,我希望它是绿色的。任何帮助都非常感激。

5 个答案:

答案 0 :(得分:2)

我不一定确定你为什么要在onDraw()中这样做。除非您有充分的理由设置自定义TextView / EditText,否则这不是必需的。

为了简化您的情况,您可以实施TextWatcher来执行此操作,在onTextChanged()中,您可以通过使用{{1来比较字符串值来设置颜色}}

以下是您理论情况的一个例子:

.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

答案 1 :(得分: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);
}

}

答案 2 :(得分:0)

您可以使用setTextColor()覆盖setText()并设置颜色。

你也可以在onDraw中进行,但它不值得重量,因为它可能会在onDraw中多次传递。

答案 3 :(得分:0)

使用此方法获取文字:

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

然后检查文本是什么并更改颜色:

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

    text.setBackgroundColor(yourcolor);
}

答案 4 :(得分:0)

您可以实施TextWatcher并使用onTextChanged()

Android Docs

中有关于此的更多信息