Надо сохранить измененный цвет нескольких букв. Я создал слайд текстов с помощью массива, проблема в том что при переходе на другой текст или при клике назад, измененный цвет пропадает. Надо что бы цвет остался. Можете подсказать что делать.
Вот код:
public class MainActivity extends AppCompatActivity {
String[] mTestArray;
int texts = 0;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTestArray = getResources().getStringArray(R.array.colors);
ImageButton next = (ImageButton)findViewById(R.id.imageButton);
ImageButton prev = (ImageButton)findViewById(R.id.imageButton2);
final TextView tex = (TextView)findViewById(R.id.textview);
final Spannable text = new SpannableString(mTestArray[2]);
text.setSpan(new ForegroundColorSpan(Color.GREEN), 0, 18, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
tex.setText(text);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(texts == mTestArray.length-1 ){
texts=0;
}else {
texts++;
}
tex.setText(mTestArray[texts]);
}
});
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(texts == 0){
tex.setText(mTestArray[0]);
}else {
texts--;
}
tex.setText(mTestArray[texts]);
}
});}
}
string.xml:
<resources>
<string name="app_name">ArrayTextColor2</string>
<string name="slidestring">Slide</string>
<string-array name="colors">
<item>I read the letter. Stood up. Sat down. Pondered for a minute. Then reread the letter again.
Я прочитал письмо. Встал. Сел. Поразмышлял минуту. Потом еще раз перечитал письмо.</item>
<item>It is only with the heart that one can see rightly.
По-настоящему можно видеть только сердцем.</item>
<item>One can become a writer only if he is talented.
Писателем может стать только тот, у кого есть талант.</item>
<item>They say, the winter will be cold.
Говорят, зима будет холодной.</item>
</string-array>
</resources>