我有7个TextViews
和一个SeekBar
我有以下代码,当我在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
是绿色而不是最后一个。知道怎么解决吗?
答案 0 :(得分:3)
您应该将==
更改为>=
更新回答:
在xml中,您有android:max="120"
,将其设为android:max="190"
答案 1 :(得分:1)
在上面的代码中你使用了 else if ,那么当第一个 if 或任何 else if 变为true时会发生什么然后在下面其他如果的其余部分未评估。你得到相同的颜色。尝试在所有条件语句中使用 if ,您将获得所需的结果。我测试了它:)
编辑:添加代码以使其更清晰
MainActivity.java
package com.abc.def;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends Activity implements
SeekBar.OnSeekBarChangeListener {
SeekBar sb;
TextView tv1, tv2, tv3, tv4, tv5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sb = (SeekBar) findViewById(R.id.seekBar1);
sb.setOnSeekBarChangeListener(this);
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
tv5 = (TextView) findViewById(R.id.textView5);
}
@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;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage("Wanna go to www.moma.org for more info?");
// add a neutral button to the alert box and assign a click listener
alertbox.setNegativeButton("NOPE!!",
new DialogInterface.OnClickListener() {
// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
}
});
alertbox.setPositiveButton("YEAH!!",
new DialogInterface.OnClickListener() {
// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
String url = "http://www.moma.org";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
// show it
alertbox.show();
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromTouch) {
// TODO Auto-generated method stub
switch (seekBar.getId()) {
case R.id.seekBar1:
// tvOne.setText(String.valueOf(progress));
if (progress >= 10) {
tv1.setBackgroundColor(Color.parseColor("#00996a"));
tv2.setBackgroundColor(Color.WHITE);
tv3.setBackgroundColor(Color.WHITE);
tv4.setBackgroundColor(Color.WHITE);
tv5.setBackgroundColor(Color.WHITE);
}if (progress >= 20) {
tv1.setBackgroundColor(Color.WHITE);
tv2.setBackgroundColor(Color.parseColor("#00996a"));
tv3.setBackgroundColor(Color.WHITE);
tv4.setBackgroundColor(Color.WHITE);
tv5.setBackgroundColor(Color.WHITE);
}if (progress >= 40) {
tv1.setBackgroundColor(Color.WHITE);
tv2.setBackgroundColor(Color.WHITE);
tv3.setBackgroundColor(Color.parseColor("#00996a"));
tv4.setBackgroundColor(Color.WHITE);
tv5.setBackgroundColor(Color.WHITE);
}if (progress >= 60) {
tv1.setBackgroundColor(Color.WHITE);
tv2.setBackgroundColor(Color.WHITE);
tv3.setBackgroundColor(Color.WHITE);
tv4.setBackgroundColor(Color.parseColor("#00996a"));
tv5.setBackgroundColor(Color.WHITE);
}if (progress >= 70) {
tv1.setBackgroundColor(Color.WHITE);
tv2.setBackgroundColor(Color.WHITE);
tv3.setBackgroundColor(Color.WHITE);
tv4.setBackgroundColor(Color.WHITE);
tv5.setBackgroundColor(Color.parseColor("#00996a"));
}if (progress >= 80) {
tv1.setBackgroundColor(Color.WHITE);
tv2.setBackgroundColor(Color.WHITE);
tv3.setBackgroundColor(Color.WHITE);
tv4.setBackgroundColor(Color.WHITE);
tv5.setBackgroundColor(Color.parseColor("#00996a"));
}if (progress >= 100) {
tv1.setBackgroundColor(Color.WHITE);
tv2.setBackgroundColor(Color.WHITE);
tv3.setBackgroundColor(Color.WHITE);
tv4.setBackgroundColor(Color.WHITE);
tv5.setBackgroundColor(Color.WHITE);
}
break;
default:
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/verti"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/beach1"
android:baselineAligned="false"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="100"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="60"
android:orientation="vertical"
android:weightSum="100" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="200sp"
android:layout_weight="50"
android:background="@android:color/white"
android:text="" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="200sp"
android:layout_marginTop="23dp"
android:layout_weight="50"
android:background="@android:color/darker_gray"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="40"
android:orientation="vertical"
android:weightSum="100" >
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="158dp"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:background="#FFEE66"
android:text="" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="#AABB66"
android:text="" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="#ee1166"
android:text="" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:max="100"
android:progress="0"
android:background="@android:color/black"/>
</LinearLayout>
</LinearLayout>