如何在更改seekbar android时更改textview的颜色?
问题描述:
我使用seekbar来改变textview的颜色,但问题是在seekbar的特定位置点击时,textview颜色的其他位置没有改变。 如何解决这个问题?如何在更改seekbar android时更改textview的颜色?
在此先感谢。
这是代码。
public class SplashActivity extends Activity {
private TextView textView;
private SeekBar seekBar;
int p = 0, noOfLength;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.test);
textView = (TextView) findViewById(R.id.textView1);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
textView.setText("Loading.Please wait...");// length of string is 22
String textLength = textView.getText().toString();
noOfLength = textLength.length();
seekBar.setMax(noOfLength);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
try {
Spannable spannableString = new SpannableString(textView
.getText());
if (progress > p) {
p = progress;
spannableString.setSpan(new ForegroundColorSpan(
Color.YELLOW), 0, progress,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
} else {
p = progress;
spannableString.setSpan(new ForegroundColorSpan(
Color.GRAY), progress - 1, progress + 1,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
当我不留,从开始到结束,从年底开始位置移动搜索条,它的工作原理,但是当我点击特定的位置搜索条,它不工作。
这是问题的截图:
答
如果你愿意单个字符颜色以颜色,而移动搜索条,然后把这个在onProgressChanged:如果你想
try {
Spannable spannableString = new SpannableString(textView
.getText());
if (progress > p) {
p = progress;
spannableString.setSpan(new ForegroundColorSpan(
Color.YELLOW), progress-1, progress,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
spannableString.setSpan(new ForegroundColorSpan(
Color.GRAY), 0, progress-1,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
} else {
p = progress;
spannableString.setSpan(new ForegroundColorSpan(
Color.YELLOW), progress-1, progress,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
spannableString.setSpan(new ForegroundColorSpan(
Color.GRAY), progress, noOfLength,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
}
} catch (Exception e) {
e.printStackTrace();
}
或颜色所有字符然后试试这个:
try {
Spannable spannableString = new SpannableString(textView
.getText());
if (progress > p) {
p = progress;
spannableString.setSpan(new ForegroundColorSpan(
Color.YELLOW), 0, progress,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
} else {
p = progress;
spannableString.setSpan(new ForegroundColorSpan(
Color.GRAY), progress - 1, noOfLength,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
}
} catch (Exception e) {
e.printStackTrace();
}
+0
优秀!非常感谢 – Shadow
使用p不是这个spannableString.setSpan中的进度(新的ForegroundColorSpan( Color.YELLOW),0,进度, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); –
为什么不禁止用户在seekbar上的触摸? –
我尝试了p。但仍然是同样的问题@AvishekDas – Shadow