java - 为什么 setText() 在恢复片段状态时不起作用
问题描述
我正在尝试完成一个井字游戏应用程序的编码,该应用程序使用相同片段的两个实例一起通信,以便每个玩家都可以在其自己的网格上播放,如此处所示(每个网格都放置在一个 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()
不能设置保存的文本
编辑#1
根据要求,这是用于获取按钮 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里面有9个按钮LinearLayouts
解决方案
好的,经过一些测试,我发现在任何方向更改后,托管片段的活动将重新启动并初始化 2 个新片段,因此setText
将文本设置在旧片段而不是当前显示的片段上,这也解释了为什么android:freezesText="true"
不起作用. 所以我修改onCreate
为MainActivity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null) {
fragment1 = FragmentTris.newInstance(1)/*new FragmentTris(1)*/;
fragment2 = FragmentTris.newInstance(2)/*new FragmentTris(2)*/;
getSupportFragmentManager().beginTransaction()
.replace(R.id.container1, fragment1, fragment1_tag)
.replace(R.id.container2, fragment2, fragment2_tag)
.commit();
}
else if(savedInstanceState != null)
{
fragment1 = (FragmentTris) getSupportFragmentManager().findFragmentByTag(fragment1_tag);
fragment2 = (FragmentTris) getSupportFragmentManager().findFragmentByTag(fragment2_tag);
}
if(!fragment1.isInLayout() && !fragment2.isInLayout())
{
getSupportFragmentManager().beginTransaction()
.replace(R.id.container1, fragment1, fragment1_tag)
.replace(R.id.container2, fragment2, fragment2_tag)
.commit();
}
}
这样在旋转后可以使用相同的片段实例并正确恢复按钮上的文本
推荐阅读
- c# - .ASP.NET Core 仅在控制器上存在策略时运行策略处理程序
- javascript - 有没有办法检查 JS 中的字符串是否是一个表情符号?
- android - searchView onbackpressed 视图问题
- java - JAVA Swing:无法将文本区域添加到边框布局
- java - 如何更改未选中的 JavaFX ListCell 的背景颜色
- javascript - 具有 20 多个属性的双向计算属性 (get,set)
- java - 将 SpringBoot 项目部署到 WLS,动态 Web 模块不断恢复到 4.0
- file - Xamarin 表单无法获取文件位置
- python - Python - 如何在列表值的列上使用 sum 函数而不会无意连接
- java - Android:有没有办法将一个 byte[] 图像分成两个然后拉伸它们?