更改seekbar android时如何更改textview的颜色?

时间:2015-04-07 06:33:33

标签: android textview spannablestring android-seekbar

我使用搜索栏来改变textview的颜色,但问题是在搜索栏中点击特定位置时,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();
            }

        }
    });

}

}

当我移动搜索栏而不是从开始到结束以及从结束到开始位置时,它可以工作但是当我在特定位置点击搜索栏时,它不起作用。

以下是问题的截图:

enter image description here

1 个答案:

答案 0 :(得分:2)

如果您愿意在移动搜索栏时将单个字符着色为彩色,请将其放入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();
            }