gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:38:40 28 4
gpt4 key购买 nike

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com