我正在努力保存几个小时。恢复TextView和按钮。似乎我无法保存TextView,因为“outState”需要一个String。
我制作了一个TicTacToe游戏,但我希望在方向为“风景”时保存所有内容
以下是我正在尝试做的事情的一部分:
private Button lesBoutons[];
private Button rejouer;
private Jeu jeu;
private TextView leTexte;
private int[] tabGagne;
public boolean fini;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.PutString("savText", leTexte);
//TextView savTexte = leTexte;
//String phrase=leTexte.toString();
//outState.putString("TEST", phrase);
//leTexte.getResources();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// leTexte.setText(savedInstanceState.getString("TEST"));
}
如何保存此TextView和所有按钮?
答案 0 :(得分:6)
Android在销毁您的活动之前调用onSaveInstanceState()
,以便您可以保存有关应用程序状态的数据。然后,您可以在onCreate()
或onRestoreInstanceState()
期间恢复状态。
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("savText", leTexte.getText().toString());
//save the state
//1st parameter is the key
//2nd parameter is the value
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
leTexte.setText(savedInstanceState.getString("savText"));
//restore it using the key
}
另请看下面的链接。
http://developer.android.com/guide/topics/resources/runtime-changes.html
答案 1 :(得分:3)
做leTexte.getText()
。这将为您提供文本,但请确保首先初始化TextView对象。