根据SeekBar的进度更改TextView颜色 [英] Changing the TextView color depending on the progress of a SeekBar

查看:369
本文介绍了根据SeekBar的进度更改TextView颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变 TextView 的颜色取决于 SeekBar 的进度如下: / p>


  • 0-25% - >绿色

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



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



我非常感谢任何帮助!



代码:

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






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


解决方案

我没有得到什么问题,我工作,它对我来说非常好。我已经附上了出来的屏幕截图。请检查。



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 arg0){
// TODO自动生成的方法存根

}

public void onStartTrackingTouch(SeekBar arg0){
// TODO自动生成方法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<
aksharTextView.setTextColor(Color.WHITE);
else if(progress> = 75&& progress< = 100)
aksharTextView.setTextColor(Color.GREEN);
else
aksharTextView.setTextColor(Color.RED);
}
});

输出画面










I am trying to change the color of the TextView depending on the SeekBar's progress as following:

  • 0-25% -> green
  • 25%-50% -> yellow and so on...

When I using the following code, the TextView doesn't show the value anymore.

I would greatly appreciate any help!

Code:

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

解决方案

I don't get what the problem is, but I worked on it and it works very fine for me. I have attached the screen shots of the out put also. Please check.

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 Screens

这篇关于根据SeekBar的进度更改TextView颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆