如何在更改 seekbar android 时更改 textview 的颜色?
How to change color of textview when changing seekbar android?
我正在使用 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();
}
我正在使用 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();
}