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

标签 android textview spannablestring android-seekbar

我正在使用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();
            }

        }
    });

}

}

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

这是问题的屏幕截图:

enter image description here

最佳答案

如果您愿意在移动搜索栏时将单个字符着色,则将其放入 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/

相关文章:

Java onCreate 带有参数,如 initWithParameter c++

Android - 如何从 Android 设备打印?

android - 如何从 SpannableString 中删除跨度样式

java - Json.getString() 上的 NullPointerException

android - 用于代码生成的 ANTLR Tool 版本 4.5.3 与当前运行时版本 4.7.1 不匹配

android - 如何以编程方式使用默认的 AlertDialog 添加 TextView 和 EditText

ios - UITextView 不是从 iOS 中的第一行开始

android - 我有 edittext 填充问题

android - 文本在 EditText 中被 ImageSpan 弄乱了

android - 如何在 jetpack 中添加 ImageSpan 撰写文本