我正在尝试完成井字游戏应用程序的编码,该应用程序使用相同片段的两个实例进行通信,以便每个玩家都可以在其自己的网格as show here上播放(每个网格都放置在xml文件中,该文件替换了主xml)。该应用程序正常运行,但是旋转后我无法恢复该应用程序的状态。为此,我使用位于另一个名为ArrayList
的类中的静态ArrayPersistence
:
private static ArrayList<String> stringArrayList;
public ArrayPersistence(){}
public static void setStringArrayList(String[][] buttons)
{
stringArrayList = new ArrayList<>(9);
for(int i = 0; i < 3; i++)
for(int y = 0; y < 3; y++)
{
stringArrayList.add(buttons[i][y]);
}
//System.out.println(stringArrayList);
}
public static ArrayList<String> getStringArrayList()
{
//System.out.println(stringArrayList);
return stringArrayList;
}
使用方法onSaveInstanceState
,我保存了按钮上显示的文本:
@Override
public void onSaveInstanceState(Bundle b)
{
String[][] string = new String[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
string[i][j] = buttons[i][j].getText().toString();
}
}
ArrayPersistence.setStringArrayList(string);
int y = 1;
b.putInt("rebooted", y);
super.onSaveInstanceState(b);
}
使用onActivityCreated
我尝试恢复文本
@Override
public void onActivityCreated(Bundle b)
{
super.onActivityCreated(b);
if(b != null)
{
if (b.getInt("rebooted") == 1)
{
ArrayList<String> string = ArrayPersistence.getStringArrayList();
System.out.println("Arraylist printed by onActivityCreated: "+string);
int i = 0;
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
{
i++
String s = string.get(i);
buttons[x][y].setText(s);//<--- Problem here
}
}
}
}
二维数组用于存储九个按钮的ID。该应用在编译或运行时未显示任何错误,我只是无法在按钮上设置保存的文本。我添加了一些system.out以打印整个ArrayList
,以确保所有内容均已正确保存和检索,但无法弄清楚为什么setText()
无法设置已保存的文本
根据要求,这是用于获取按钮ID的代码
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.layout_tris, container, false);
this.ID = getArguments().getInt("id");
buttons[0][0] = v.findViewById(R.id.b1_1);
buttons[0][0].setOnClickListener(this);
buttons[0][1] = v.findViewById(R.id.b1_2);
buttons[0][1].setOnClickListener(this);
buttons[0][2] = v.findViewById(R.id.b1_3);
buttons[0][2].setOnClickListener(this);
buttons[1][0] = v.findViewById(R.id.b2_1);
buttons[1][0].setOnClickListener(this);
buttons[1][1] = v.findViewById(R.id.b2_2);
buttons[1][1].setOnClickListener(this);
buttons[1][2] = v.findViewById(R.id.b2_3);
buttons[1][2].setOnClickListener(this);
buttons[2][0] = v.findViewById(R.id.b3_1);
buttons[2][0].setOnClickListener(this);
buttons[2][1] = v.findViewById(R.id.b3_2);
buttons[2][1].setOnClickListener(this);
buttons[2][2] = v.findViewById(R.id.b3_3);
buttons[2][2].setOnClickListener(this);
return v;
}
这是按钮xml:
<Button
android:id="@+id/b1_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#000000"
android:textSize="30dp" />
[3 LinearLayouts
内有9个按钮
您可以尝试使用onViewStateRestored(Bundle savedInstanceState)
的onViewStateRestored(Bundle savedInstanceState)
生命周期方法。
这似乎是一个问题:
[确定,经过一些测试,我发现在改变方向后,承载片段的活动将重新启动并初始化2个新片段,因此true
应当在旧片段上设置文本,而不是当前显示的片段,这也可以解释为什么<Button
android:id="@+id/b1_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:freezesText="true" <---- add this
android:textColor="#000000"
android:textSize="30dp" />
无效。因此,我将setText
中的android:freezesText="true"
功能修改为: