0

onDraw を使用して、テキスト値に応じて色が変わるカスタム テキスト ビューを作成したいと考えています。たとえば、テキスト値が「こんにちは」の場合は赤、「さようなら」の場合は緑にしたいとします。どんな助けでも大歓迎です。

4

5 に答える 5

2

でこれを実行する理由が必ずしもわかりません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
}
于 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

TextWatcher を実装して使用できますonTextChanged()

詳細については、こちらのAndroid ドキュメントをご覧ください

于 2012-12-06T18:40:33.387 に答える
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 に答える