java - 当 SeekBar 传递值时更改 TextView 颜色

标签 java android seekbar

我有 7 个 TextView 和 1 个 SeekBar

enter image description here

我有以下代码,当我在 SeekBar 中移动拇指时,应该更改文本颜色:

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener {

    SeekBar sbOne, sbTwo;
    SeekBar sbSyst;
    TextView tvOne;
    TextView tvSOne, tvSTwo, tvSThree, tvSFour, tvSFive, tvSSix, tvSSeven;
    NumberPicker npO, npT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        sbSyst = (SeekBar) findViewById(R.id.syst_bar);
        sbSyst.setOnSeekBarChangeListener(this);

        sbOne = (SeekBar) findViewById(R.id.sb1);
        sbOne.setOnSeekBarChangeListener(this);

        sbTwo = (SeekBar) findViewById(R.id.sb2);
        sbTwo.setOnSeekBarChangeListener(this);

        tvOne = (TextView) findViewById(R.id.tv1);

        tvSOne = (TextView) findViewById(R.id.tvFirst);
        tvSTwo = (TextView) findViewById(R.id.tvSecond);
        tvSThree = (TextView) findViewById(R.id.tvThird);
        tvSFour = (TextView) findViewById(R.id.tvFourth);
        tvSFive = (TextView) findViewById(R.id.tvFifth);
        tvSSix = (TextView) findViewById(R.id.tvSixth);
        tvSSeven = (TextView) findViewById(R.id.tvSeventh);

        /*npO = (NumberPicker) findViewById(R.id.np1);
        npO.setMinValue(70);
        npO.setMaxValue(190);
        npO.setWrapSelectorWheel(true);
        npO.setOnValueChangedListener(new OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                // do something here
                tvOne.setText(String.valueOf(newVal));
            }
        });

        npT = (NumberPicker) findViewById(R.id.np2);
        npT.setMinValue(40);
        npT.setMaxValue(100);
        npT.setWrapSelectorWheel(true);*/

    }

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {

        //tvSColor.setBackgroundColor(getColorFromSeekbars());

    progress += 70;

        switch(seekBar.getId()) {
        case R.id.sb1:
            //etO.setText(String.valueOf(progress+70));
            //npO.setValue(progress+70);
            break;
        case R.id.sb2:
            //etT.setText(String.valueOf(progress+40));
            //npT.setValue(progress+40);
            break;
        case R.id.syst_bar:
            tvOne.setText(String.valueOf(progress));
            if (progress == 70) {
                tvSOne.setTextColor(Color.parseColor("#00996a"));
                tvSTwo.setTextColor(Color.WHITE);
                tvSThree.setTextColor(Color.WHITE);
                tvSFour.setTextColor(Color.WHITE);
                tvSFive.setTextColor(Color.WHITE);
                tvSSix.setTextColor(Color.WHITE);
                tvSSeven.setTextColor(Color.WHITE);
            }
            else if (progress == 90) {
                tvSOne.setTextColor(Color.WHITE);
                tvSTwo.setTextColor(Color.parseColor("#00996a"));
                tvSThree.setTextColor(Color.WHITE);
                tvSFour.setTextColor(Color.WHITE);
                tvSFive.setTextColor(Color.WHITE);
                tvSSix.setTextColor(Color.WHITE);
                tvSSeven.setTextColor(Color.WHITE);
            }
            else if (progress == 110) {
                tvSOne.setTextColor(Color.WHITE);
                tvSTwo.setTextColor(Color.WHITE);
                tvSThree.setTextColor(Color.parseColor("#00996a"));
                tvSFour.setTextColor(Color.WHITE);
                tvSFive.setTextColor(Color.WHITE);
                tvSSix.setTextColor(Color.WHITE);
                tvSSeven.setTextColor(Color.WHITE);
            }
            else if (progress == 130) {
                tvSOne.setTextColor(Color.WHITE);
                tvSTwo.setTextColor(Color.WHITE);
                tvSThree.setTextColor(Color.WHITE);
                tvSFour.setTextColor(Color.parseColor("#00996a"));
                tvSFive.setTextColor(Color.WHITE);
                tvSSix.setTextColor(Color.WHITE);
                tvSSeven.setTextColor(Color.WHITE);
            }
            else if (progress == 150) {
                tvSOne.setTextColor(Color.WHITE);
                tvSTwo.setTextColor(Color.WHITE);
                tvSThree.setTextColor(Color.WHITE);
                tvSFour.setTextColor(Color.WHITE);
                tvSFive.setTextColor(Color.parseColor("#00996a"));
                tvSSix.setTextColor(Color.WHITE);
                tvSSeven.setTextColor(Color.WHITE);
            }
            else if (progress == 170) {
                tvSOne.setTextColor(Color.WHITE);
                tvSTwo.setTextColor(Color.WHITE);
                tvSThree.setTextColor(Color.WHITE);
                tvSFour.setTextColor(Color.WHITE);
                tvSFive.setTextColor(Color.WHITE);
                tvSSix.setTextColor(Color.parseColor("#00996a"));
                tvSSeven.setTextColor(Color.WHITE);
            }
            else if (progress == 190) {
                tvSOne.setTextColor(Color.WHITE);
                tvSTwo.setTextColor(Color.WHITE);
                tvSThree.setTextColor(Color.WHITE);
                tvSFour.setTextColor(Color.WHITE);
                tvSFive.setTextColor(Color.WHITE);
                tvSSix.setTextColor(Color.WHITE);
                tvSSeven.setTextColor(Color.parseColor("#00996a"));
            }
            //etT.setText(String.valueOf(progress+70));
            //npT.setValue(progress+40);
            break;
        default:
            break;
        }
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        //mTrackingText.setText("Tracking on");
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        //tvOne.setText("Red: " + String.valueOf(progress));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

XML:

<LinearLayout
    android:id="@+id/refresh_match_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/background_round"
    android:orientation="vertical"
    android:visibility="visible"
    android:layout_alignParentBottom="true" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Systolic (top number)"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <SeekBar
        android:id="@+id/syst_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:progress="0"
        android:max="190"
        android:progressDrawable="@drawable/progress_bar"
        android:secondaryProgress="0"
        android:thumb="@drawable/thumb_state" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="horizontal"
        android:visibility="visible"
        android:weightSum="14" >

        <TextView
            android:id="@+id/tvFirst"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center_horizontal"
            android:text="70"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvSecond"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center_horizontal"
            android:text="90"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvThird"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center_horizontal"
            android:text="110"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvFourth"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center_horizontal"
            android:text="130"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvFifth"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center_horizontal"
            android:text="150"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvSixth"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center_horizontal"
            android:text="170"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvSeventh"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center_horizontal"
            android:text="190"
            android:textColor="@android:color/white"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

我可能做得不正确,但正如您从图像中看到的 progress 的值是 190,但第三个 TextView 是绿色的,而不是最后一个。知道如何解决吗?

最佳答案

您可能应该将 == 更改为 >=

更新的答案:

在 xml 中,您有 android:max="120",将其设为 android:max="190"

关于java - 当 SeekBar 传递值时更改 TextView 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20792887/

相关文章:

java - 扩展父类中私有(private)成员的层次结构

android - Flutter 使用 C/C++ 代码构建 - 示例?

java - libgdx,如何使我的图像可触摸

avfoundation - AVPlayer Slider 寻求向后问题

android - 更改默认搜索栏颜色

android - 无法将自定义拇指定位在搜索栏的中心

java - 如何使用 Java 将包含 JSON 对象的字符串转换为实际的 JSON

java - 将静态方法放在接口(interface)中是一种好习惯吗?

android - 编写 Android Espresso 测试

java - 将行从文件复制到另一个文件