2015-04-07 148 views
2

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

     } 
    }); 

} 

}

當我不留,從開始到結束,從年底開始位置移動搜索條,它的工作原理,但是當我點擊特定的位置搜索條,它不工作。

這是問題的截圖:

enter image description here

+0

使用p不是這個spannableString.setSpan中的進度(新的ForegroundColorSpan( Color.YELLOW),0,進度, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); –

+0

爲什麼不禁止用戶在seekbar上的觸摸? –

+0

我嘗試了p。但仍然是同樣的問題@AvishekDas – Shadow

回答

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(); 
      } 
+0

優秀!非常感謝 – Shadow

相關問題