2012-12-06 5 views
0

onDraw를 사용하여 텍스트 값에 따라 색상을 변경하는 사용자 정의 텍스트보기를 만들고 싶습니다. 예를 들어 텍스트 값이 "안녕하세요"인 경우 빨간색으로 표시하고 "안녕히"라고 말하면 녹색으로 표시합니다. 어떤 도움이 크게 감사드립니다.텍스트에 따라 TextView 색상 변경

답변

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); 
} 

}

그것이
0

setText()를 덮어 쓰고 setTextColor()를 사용하여 색상을 설정할 수 있습니다.

onDraw에서도 사용할 수 있지만 onDraw 내부에서 여러 번 전달 될 수 있으므로 그만한 가치는 없습니다.

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

그런 다음 텍스트가 무엇인지 확인하고 색상 변경 :

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

    text.setBackgroundColor(yourcolor); 
} 
0

사용이 텍스트를 얻을 수 있습니다 여기에 Android Docs

+2

은 그것을보고, 내 나는이 텍스트 뷰를 필요로하기 때문에 내가 분명히 내 질문에 표현 된 I가 된 onDraw에서이 작업을 수행 할 필요가 : – ceptno

+0

작동하지 않습니다, 당신의 if 문을 변경 – Lotzki

0

당신은 TextWatcher를 구현하고 그것에 대해 onTextChanged()

더 사용할 수 있습니다

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 
    } 
}); 

는 단순히 onTextChanged()에서 코드를 추출하고 thisyourEditText을 변경하거나에 배치, onDraw()이을 유지하기 위해 필요한 느끼는 경우 : 여기

은 이론적 인 상황의 예입니다 대신에 생성자 :

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 
} 
+0

나쁜 커스텀의 listview 행 항목이된다. – AndroidDev

+0

글쎄, 당신은 그것에 대한 사용자 정의'TextView' 필요는 없지만 정확히 동일한 코드를 사용할 수 있으며이'TextWatcher'를 생성자의'TextView'에 첨부 할 수 있습니다. – Eric

+1

@ DroidDevAge13 위의 내용을 설명하기 위해 내 게시물을 편집했습니다. 다행스럽게도 충분히 명확하다 ..;) – Eric