根据SeekBar的进度更改TextView颜色

时间:2012-06-15 08:31:49

标签: android xml colors textview seekbar

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

  • 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"
    />

1 个答案:

答案 0 :(得分:2)

我不知道问题是什么,但我做了它,它对我来说非常好。我也附上了输出的屏幕截图。请检查。

<强> main.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);
            }
        });

OutPut屏幕

One

enter image description here

enter image description here

enter image description here

相关问题