如何更改仅满足条件的文本视图的颜色?

huangapple go评论96阅读模式
英文:

How to change the color of only textviews that meet a condition?

问题

基本上,我有一个列表,我想要的是,对于满足条件的文本视图,它们的颜色会有所不同。

  1. public class CustomTempAdapter extends ArrayAdapter<String> {
  2. private Context mContext;
  3. private int id;
  4. private List<String> items;
  5. public CustomTempAdapter(Context context, int textViewResourceId, List<String> list) {
  6. super(context, textViewResourceId, list);
  7. mContext = context;
  8. id = textViewResourceId;
  9. items = list;
  10. }
  11. @Override
  12. public View getView(int position, View v, ViewGroup parent) {
  13. View mView = v;
  14. if (mView == null) {
  15. LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  16. mView = vi.inflate(id, null);
  17. }
  18. TextView text = (TextView) mView.findViewById(R.id.tempView);
  19. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  20. items.forEach(item -> {
  21. if (Double.parseDouble(item) > 100.00) {
  22. text.setBackgroundColor(Color.RED);
  23. int color = Color.argb(200, 255, 64, 64);
  24. text.setBackgroundColor(color);
  25. }
  26. });
  27. }
  28. return mView;
  29. }
  30. }
  1. CustomTempAdapter adapter = new CustomTempAdapter(this, R.layout.customTextView, normalStrings);
  2. ListView list = findViewById(R.id.customListView);
  3. list.setAdapter(adapter);

目前只显示空白的文本视图。我尝试了不同的方法。这次我使用了一个普通的 ArrayAdapter<String>,然后每当添加一个项目时:

  1. View v;
  2. for (int i = 0; i < list.getChildCount(); i++) {
  3. v = list.getChildAt(i);
  4. TextView tv = v.findViewById(R.id.tempView);
  5. if (tv != null && Double.parseDouble(tv.getText().toString()) > 100)
  6. tv.setTextColor(Color.parseColor("#FF0000"));
  7. }

在这个方法中,我遍历所有文本视图,并尝试改变它们的颜色。但这并不起作用。

英文:

Well basically I have a list and what I want is that for textviews that meet a condition, their color would be different.

  1. public class CustomTempAdapter extends ArrayAdapter&lt;String&gt; {
  2. private Context mContext;
  3. private int id;
  4. private List&lt;String&gt; items;
  5. public CustomTempAdapter(Context context, int textViewResourceId, List&lt;String&gt; list) {
  6. super(context, textViewResourceId, list);
  7. mContext = context;
  8. id = textViewResourceId;
  9. items = list;
  10. }
  11. @Override
  12. public View getView(int position, View v, ViewGroup parent) {
  13. View mView = v;
  14. if (mView == null) {
  15. LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  16. mView = vi.inflate(id, null);
  17. }
  18. TextView text = (TextView) mView.findViewById(R.id.tempView);
  19. if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.N) {
  20. items.forEach(item -&gt; {
  21. if (Double.parseDouble(item) &gt; 100.00) {
  22. text.setBackgroundColor(Color.RED);
  23. int color = Color.argb(200, 255, 64, 64);
  24. text.setBackgroundColor(color);
  25. }
  26. });
  27. }
  28. return mView;
  29. }
  30. }
  1. CustomTempAdapter adapter = new CustomTempAdapter(this, R.layout.customTextView, normalStrings);
  2. ListView list = findViewById(R.id.customListView);
  3. list.setAdapter(adapter);

Currently this is only displaying blank textviews. I tried a different approach. This time using a normal ArrayAdapter&lt;String&gt;, then whenever an item is added:

  1. View v;
  2. for (int i = 0; i &lt; list.getChildCount(); i++) {
  3. v = list.getChildAt(i);
  4. TextView tv = v.findViewById(R.id.tempView);
  5. if (tv != null &amp;&amp; Double.parseDouble(tv.getText().toString()) &gt; 100)
  6. tv.setTextColor(Color.parseColor(&quot;#FF0000&quot;));
  7. }

In this approach, I iterate through all the textviews and try to change their color. It does not work.

答案1

得分: 0

你可以将getView方法重写如下:

  1. public View getView(int position, View v, ViewGroup parent) {
  2. View mView = v;
  3. if (mView == null) {
  4. LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  5. mView = vi.inflate(id, null);
  6. }
  7. TextView text = (TextView) mView.findViewById(R.id.tempView);
  8. Double value = Double.parseDouble(items.get(position));
  9. if (value > 100.00) {
  10. int color = Color.argb(200, 255, 64, 64);
  11. text.setBackgroundColor(color);
  12. } else {
  13. text.setBackgroundColor(Color.GREEN);
  14. }
  15. return mView;
  16. }
英文:

You could rewrite getView method as following:

  1. public View getView(int position, View v, ViewGroup parent) {
  2. View mView = v;
  3. if (mView == null) {
  4. LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  5. mView = vi.inflate(id, null);
  6. }
  7. TextView text = (TextView) mView.findViewById(R.id.tempView);
  8. Double value = Double.parseDouble(items.get(position));
  9. if (value &gt; 100.00) {
  10. int color = Color.argb(200, 255, 64, 64);
  11. text.setBackgroundColor(color);
  12. } else {
  13. text.setBackgroundColor(Color.GREEN);
  14. }
  15. return mView;
  16. }

huangapple
  • 本文由 发表于 2020年8月27日 08:54:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63607659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定