onDraw를 사용하여 텍스트 값에 따라 색상을 변경하는 사용자 정의 텍스트보기를 만들고 싶습니다. 예를 들어 텍스트 값이 "안녕하세요"인 경우 빨간색으로 표시하고 "안녕히"라고 말하면 녹색으로 표시합니다. 어떤 도움이 크게 감사드립니다.텍스트에 따라 TextView 색상 변경
답변
나는 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);
}
사용이 텍스트를 얻을 수 있습니다 여기에 Android Docs
당신은 TextWatcher를 구현하고 그것에 대해 onTextChanged()
더 사용할 수 있습니다
나는 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()
에서 코드를 추출하고 this
에 yourEditText
을 변경하거나에 배치, 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
}
나쁜 커스텀의 listview 행 항목이된다. – AndroidDev
글쎄, 당신은 그것에 대한 사용자 정의'TextView' 필요는 없지만 정확히 동일한 코드를 사용할 수 있으며이'TextWatcher'를 생성자의'TextView'에 첨부 할 수 있습니다. – Eric
@ DroidDevAge13 위의 내용을 설명하기 위해 내 게시물을 편집했습니다. 다행스럽게도 충분히 명확하다 ..;) – Eric
- 1. TextView 색상 변경
- 2. 색상 피커 변경 방법 TextView
- 3. 텍스트에 색상 효과 없음
- 4. 데이터베이스 값에 따라 행 색상 변경
- 5. 초기 열의 값에 따라 테이블 색상 변경
- 6. 셀 색상 변경 C#
- 7. TextInput 컨트롤의 선택된 텍스트에 대한 강조 색상 변경
- 8. pygtk : textview 위젯의 변환 색상
- 9. edittext의 텍스트에 텍스트 색상 지정
- 10. 색상 변경 색상 옵션
- 11. jquery를 사용하여 텍스트에 따라 이미지를 단추로 변경 하시겠습니까?
- 12. TextView 글꼴 변경
- 13. TextView 텍스트 변경
- 14. 클릭시 TextView 배경색 변경
- 15. Android textView onCreate 변경
- 16. 스레드에서 TextView 텍스트 변경
- 17. runOnUiThread, TextView 변경
- 18. 클릭시 Textview의 색상 변경
- 19. uidraw에서 색상 변경 변경
- 20. AlertDialog의 하이퍼 링크 색상 변경
- 21. 색상 변경
- 22. 변경 색상
- 23. 텍스트 뷰의 색상 변경
- 24. Zest의 노드 색상 변경
- 25. 테이블 행 글꼴 색상 변경
- 26. 클릭하면 안드로이드 변경 텍스트 색상
- 27. xamDataGrid 셀의 색상 변경
- 28. tabwidget 내에서 사용자 정의 textview 색상
- 29. 지도 아이콘 색상 변경
- 30. 입력 필드의 값에 따라 텍스트 색상 또는 배경을 변경
은 그것을보고, 내 나는이 텍스트 뷰를 필요로하기 때문에 내가 분명히 내 질문에 표현 된 I가 된 onDraw에서이 작업을 수행 할 필요가 : – ceptno
작동하지 않습니다, 당신의 if 문을 변경 – Lotzki