【问题标题】:Changing the TextView color depending on the progress of a SeekBar根据 SeekBar 的进度更改 TextView 颜色
【发布时间】:2012-06-15 08:31:49
【问题描述】:

我正在尝试根据SeekBar 的进度更改TextView 的颜色,如下所示:

  • 0-25% -> 绿色
  • 25%-50% -> 黄色等等...

当我使用以下代码时,TextView 不再显示该值。

我将不胜感激任何帮助!

代码:

public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) 
{   
    textView.setText(String.valueOf(progress + "%"));
    if(progress >= 25 && progress < 50)
        textView.setTextColor(R.color.Yellow);
    else if(progress >= 50 && progress < 75)
        textView.setTextColor(R.color.Orange);
    else if(progress >= 75 && progress <= 100)
        textView.setTextColor(R.color.Red);
    else 
        textView.setTextColor(R.color.Green);
}    

XML:

<TextView
    android:id="@+id/eT"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/tvProgress"
    android:textColor="@color/Green"
    android:textSize="25dp"
    android:textStyle="bold"
    android:layout_gravity="center"
    android:layout_marginBottom="5dp"
    />

【问题讨论】:

  • 它有什么错误吗?您确定字符串 R.color.Yellow、R.color.Orange 和 R.color.Red 具有正确的十六进制值吗?
  • 看起来您没有使用 setText 更新 textview。你必须做 textView.setText(progress);在每个 if 条件中
  • 该代码应该可以正常工作。你确定要设置右边的文字+颜色TextView
  • @Sana - 不,我没有收到任何错误。是的,字符串具有正确的十六进制值。
  • @Luksprog - 是的,它是正确的 TextView;我已经仔细检查过。

标签: android xml colors textview seekbar


【解决方案1】:

我不明白问题出在哪里,但我已经解决了它,它对我来说非常好用。我还附上了输出的屏幕截图。请检查。

ma​​in.xml

 <ScrollView  android:layout_width="wrap_content"
     android:layout_height="wrap_content">

     <RelativeLayout  android:layout_width="wrap_content"
         android:layout_height="wrap_content">



            <TextView  android:id="@+id/aksharTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="afadjklhfg"
                android:paddingTop="10dip"
                android:layout_below="@+id/arialTextView"/>



            <SeekBar  android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/aksharTextView"
                android:padding="10dip"
                android:id="@+id/seekbar"/>
     </RelativeLayout>


 </ScrollView>

onCreate()

 seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            public void onStopTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub

            }

            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub

            }

            public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
                aksharTextView.setText(progress+"");
                if(progress >= 25 && progress < 50)
                    aksharTextView.setTextColor(Color.BLUE);
                else if(progress >= 50 && progress < 75)
                    aksharTextView.setTextColor(Color.WHITE);
                else if(progress >= 75 && progress <= 100)
                    aksharTextView.setTextColor(Color.GREEN);
                else 
                    aksharTextView.setTextColor(Color.RED);
            }
        });

输出屏幕

【讨论】:

  • 非常感谢!我实际上有相同的代码并且没有尽头。我需要仔细看看,因为这肯定是一件小事。
  • 尝试使用 Color.BLUE,而不是 R.color.blue。它可能会起作用。这是唯一的区别
猜你喜欢
  • 1970-01-01
  • 2014-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多