我正在使用seekbar来更改textview的颜色,但问题是当单击seekbar中的特定位置时,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();
}
关于android - 如何在更改seekbar android时更改textview的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29485255/