gpt4 book ai didi

java - 屏幕旋转导致文字颜色变化

转载 作者:行者123 更新时间:2023-12-02 11:10:00 24 4
gpt4 key购买 nike

我的 Android 应用程序遇到了一个小问题,一旦旋转屏幕,文本颜色就会变回默认颜色。

基本上我有一个 if 语句,其中如果玩家一在选择按钮(总共 12 个按钮)时进行了移动,他们的选择将显示为特定颜色,如果不是玩家一的移动,那么一定是玩家二的选择被标记为不同的文本颜色。

@Override
public void onClick(View v) {
if (!((Button) v).getText().toString().equals("")) {
return;
}

if (playerOneMove) {
((Button) v).setText("A");
((Button) v).setTextColor(Color.parseColor("#e8e5e5"));
} else {
((Button) v).setText("Z");
((Button) v).setTextColor(Color.parseColor("#737374"));
}

...

}

上面的代码在OnCreate()中。如何使我的测试颜色保持在旋转范围内?我知道有p rotected void onSaveInstanceState(Bundle outState)protected void onRestoreInstanceState(Bundle savedInstanceState)但我如何调用其中的按钮呢?

更新

    private Button btnObj1;
private Button btnObj2;
private Button btnObj3;
private Button btnObj4;
private Button btnObj5;
private Button btnObj6;
private Button btnObj7;
private Button btnObj8;
private Button btnObj9;

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

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

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));
}
if (btnObj4 != null) {
btnObj4.setTextColor(Color.parseColor(textColor));
}
if (btnObj5 != null) {
btnObj5.setTextColor(Color.parseColor(textColor));
}
if (btnObj6 != null) {
btnObj6.setTextColor(Color.parseColor(textColor));
}
if (btnObj7 != null) {
btnObj7.setTextColor(Color.parseColor(textColor));
}
if (btnObj8 != null) {
btnObj8.setTextColor(Color.parseColor(textColor));
}
if (btnObj9 != null) {
btnObj9.setTextColor(Color.parseColor(textColor));
}
}


if (savedInstanceState != null) {
textColor = savedInstanceState.getString(TEXT_COLOR);
btnObj1.setTextColor(Color.parseColor(textColor));
btnObj2.setTextColor(Color.parseColor(textColor));
btnObj3.setTextColor(Color.parseColor(textColor));
btnObj4.setTextColor(Color.parseColor(textColor));
btnObj5.setTextColor(Color.parseColor(textColor));
btnObj6.setTextColor(Color.parseColor(textColor));
btnObj7.setTextColor(Color.parseColor(textColor));
btnObj8.setTextColor(Color.parseColor(textColor));
btnObj9.setTextColor(Color.parseColor(textColor));
}


setContentView(R.layout.activity_main_player2);

for (int i = 0; i < 3; 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("A");
textColor = "#e8e5e5";
((Button) v).setTextColor(Color.parseColor(textColor));
} else {
((Button) v).setText("Z");
textColor = "#737374";
((Button) v).setTextColor(Color.parseColor(textColor));
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {


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

super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {


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

super.onRestoreInstanceState(savedInstanceState);
}

谢谢

最佳答案

只要屏幕旋转并且您没有处理配置更改,您的 Activity 就会重新创建,因此您的 View 的所有状态都不会得到维护。如果您可以使用onSaveInstanceState来存储状态,如下所示:

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnObj = findViewById(<button id>);
if (savedInstanceState != null) {
textColor = savedInstanceState.getString(TEXT_COLOR);
btnObj.setTextColor(Color.parseColor(textColor));
}
...
}

@Override
public void onClick(View v) {
if (!((Button) v).getText().toString().equals("")) {
return;
}

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

...

}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString(TEXT_COLOR, textColor);
super.onSaveInstanceState(savedInstanceState);
}

关于java - 屏幕旋转导致文字颜色变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50672692/

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