[英]Android Spinner to change text size doesn't save state
private void onTextSizeSeekBarChange() {
final TextView tutorialText = (TextView) findViewById(R.id.tutorialText);
final SeekBar sb = (SeekBar) findViewById(R.id.seekBar);
sb.setMax(20);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int p = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
p = progress;
tutorialText.setTextSize(p);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (p < 20) {
sb.setProgress(p);
}
}
});
}
So the problem is that the code I have above changes the text size of a text view. 所以问题是我上面的代码更改了文本视图的文本大小。 However when going back and forth the state isn't saved so the text size is then reset. 但是,在来回移动时不会保存状态,因此会重置文本大小。 I am really new to android and would appreciate any guidance or help on how I can achieve this. 我真的是Android新手,不胜感激任何指导或帮助我如何实现这一目标。 I basically have a tutorial app and the text needs to change from the settings menu which has a seekbar allowing the user to select what text size they want. 我基本上有一个教程应用程序,需要从设置菜单中更改文本,该菜单有一个搜索栏,允许用户选择所需的文本大小。 Any Ideas? 有任何想法吗?
您可以通过以下方式之一:1)您可以将p的值保存在共享的首选项中,下次访问时,将检查p的值并将其设置在textview中2)您可以将值保存在onSavedInstance中,但是问题是,当用户从后台终止应用程序时,您将失去价值
if you have set sb.setMax(20);
如果已设置sb.setMax(20);
then you dose not need to 那你就不需要
if (p < 20) { sb.setProgress(p); }
code is useless user cant seek more then 20 代码无用,用户无法再查找20
you have code like that 你有这样的代码
private void onTextSizeSeekBarChange() {
final TextView tutorialText = (TextView) findViewById(R.id.tutorialText);
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(CONTEXT);
int p = SP.getInt("key", p);
tutorialText.setTextSize(p);
final SeekBar sb = (SeekBar) findViewById(R.id.seekBar);
sb.setMax(20);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
p = progress;
tutorialText.setTextSize(p);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
sb.setProgress(p);
Editor edit = SP.edit();
edit.putInt("key", p);
edit.commit();
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.