gpt4 book ai didi

java - saveInstanceState 根据之前的用户操作更改颜色

转载 作者:行者123 更新时间:2023-12-02 11:09:53 26 4
gpt4 key购买 nike

我有三个按钮,第一个选择的按钮将标记为 X,而选择的第二个按钮将标记为 O,最后一个按钮将再次标记为 X。

如果标记 X,则其颜色为白色,如果标记 O,则标记为灰色。

现在我想做的是使用保存的实例,这样当我旋转手机时,颜色保持不变。实际发生的情况是,如果我旋转手机,然后做出最新的选择,所有文本都会恢复为该颜色。

因此,如果我按 X 和 O 并旋转手机,X 和 O 都会显示为灰色,即 O 的颜色。

如果我选择最后一个 X 并旋转手机,所有字母将被标记为白色,即 X 的颜色。

我不确定是否是设置的颜色导致了它,或者是记住了之前是谁移动的并据此设置了颜色,我的问题是如何解决这个问题,以便所有字母都保持其颜色旋转?

private boolean playerOneMove = true;

private Button[][] buttons = new Button[1][3];

private static final String TEXT_COLOR = "textColor";
private String textColor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_player2);

btnObj1 = findViewById(R.id.button_00);
btnObj2 = findViewById(R.id.button_01);
btnObj3 = findViewById(R.id.button_02);

if (savedInstanceState != null) {
textColor = savedInstanceState.getString(TEXT_COLOR);

if(btnObj1 != null) {
btnObj1.setTextColor(Color.parseColor(textColor));
}
if (btnObj2 != null) {
btnObj2.setTextColor(Color.parseColor(textColor));
}
if (btnObj3 != null) {
btnObj3.setTextColor(Color.parseColor(textColor));
}

for (int i = 0; i < 1; i++) {
for (int j = 0; j < 3; j++) {
String buttonID = "button_" + i + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = findViewById(resID);
buttons[i][j].setOnClickListener(this);
}
}
}

@Override
public void onClick(View v) {

if (!((Button) v).getText().toString().equals("")) {
return;
}

if (playerOneMove) {
((Button) v).setText("X");
textColor = "#e8e5e5";
((Button) v).setTextColor(Color.parseColor(textColor));
} else {
((Button) v).setText("O");
textColor = "#737374";
((Button) v).setTextColor(Color.parseColor(textColor));
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putBoolean("playerOneMove", playerOneMove);
outState.putString(TEXT_COLOR, textColor);

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) { ;
super.onRestoreInstanceState(savedInstanceState);


playerOneMove = savedInstanceState.getBoolean("playerOneMove");
textColor = savedInstanceState.getString(TEXT_COLOR);


}

最佳答案

您看到的行为是正常的,因为您保存了一个 textColor,它始终是用户最后设置的颜色。相反,您可以简单地迭代按钮数组并保存每个按钮的文本颜色:

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 3; j++) {
String buttonID = "button_" + i + j;
Button btn = buttons[i][j];
outState.putCharSequence(buttonID, btn.getText());
}
}
}

然后在onCreate()中(去掉onRestoreInstanceState()方法)恢复按钮的状态:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_player2);
int playerX = Color.parseColor("#e8e5e5");
int playerO = Color.parseColor("#737374");
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 3; j++) {
String buttonID = "button_" + i + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = findViewById(resID);
buttons[i][j].setOnClickListener(this);
if (savedInstanceState != null) {
String btnState = savedInstanceState.getCharSequence(buttonID);
if (btnState.equals("X")) {
// this is player X
buttons[i][j].setTextColor(playerX);
} else if (btnState.equals("O")) {
// this is player O
buttons[i][j].setTextColor(playerO);
} else {
// unclicked btn, do you have another color?
}
}
}
}

如果您有更多按钮,则将按钮状态分组并保存在列表中可能更有意义,而不是单独保存按钮状态。

关于java - saveInstanceState 根据之前的用户操作更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50685842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com