旋转屏幕会导致文本颜色发生变化

时间:2018-06-04 02:19:01

标签: java android

我的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);
    }

由于

1 个答案:

答案 0 :(得分:1)

每当旋转屏幕并且您没有处理配置更改时,您的活动将被重新创建,因此您的视图的所有状态都将得不到维护。如果您可以使用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);
}