android旋转屏幕导致文本颜色更改为默认值 [英] android rotating screen causes text colour to change to default

查看:141
本文介绍了android旋转屏幕导致文本颜色更改为默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用程序中我正在创建一个tic tac toe游戏,我在下面有这个代码,如果它是玩家一个移动然后将他们的选择设置为具有特定颜色的 X ,否则它必须是播放器2因此将文本设置为 O ,以便选择不同的颜色。

In my android app where I am creating a tic tac toe game, I have this code below where if it's player one move then set their selection as X with a particular colour, else it must be player 2 so set text as O for their selection with a different colour.

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

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

    ...

}

我遇到了一个问题,那就是我旋转屏幕的时候。当我旋转屏幕时,X和O的文本都变为android studio提供的默认文本颜色。我想保留这些文字的颜色,但我不知道该怎么做?我确定没有全局文本颜色集。

I have a problem though and it is in regards to when I rotate the screen. When I rotate the screen, the text for X and O both change to the default text colour android studio provides. I want to keep the colours for these text but I am not sure how to do that? I have made sure there is no global text colour set.

以下是处理方向更改的代码:

Below is my code that handles orientation changes:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean("playerOneMove", playerOneMove);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    playerOneMove = savedInstanceState.getBoolean("playerOneMove");
}


推荐答案

你需要保存颜色你的tic tac toe board的每一个盒子,并在你的布局配置变化(即设备旋转)上再次保留它们。

You need to save the colours of each box of your tic tac toe board and retain them again on your layout configuration changes (i.e. device rotation).

您可以考虑查看在此处回答,以详细解释您的问题。您可以在此处查看开发人员文档,以便处理配置更改同样。

You might consider looking into the answer here for a detailed explanation of your problem. You might check the developer documentation here for handling the configuration changes as well.

关键的想法是将布局状态保存在配置发生变化的变量中,并在 onCreate 或 onCreateView 函数分别为活动片段 。但是,在您的情况下,您需要存储大量数据,并且在每次配置更改时,您需要再次还原它们,这不是一种有效的方法。我建议您寻找其他可用的选项,这些选项可以在布局的方向或配置更改后继续使用。

The key idea is to save the layout statues in variables which survives the configuration changes and update them accordingly in your onCreate or onCreateView function in case of Activity and Fragment respectively. However, in your case, you need to store a lot of data and on each configuration change, you need to restore them again which is not an efficient way to do that. I would like to recommend you look for other available options which survive the orientation or configuration changes of your layout.

我强烈建议实施 ViewModel ,它可以在应用程序配置更改后继续存在,并以最有效的方式处理整个UI表示。我们的想法是将您的UI元素与 ViewModel 绑定,然后每次从 ViewModel 中保留UI元素。它可以保留在准确状态,直到活动片段结束。

I would strongly suggest implementing ViewModel in your case, which survives the application configuration change and handles the overall UI representation in the most effective way. The idea is to bind your UI elements with your ViewModel and then retain the UI elements each time from your ViewModel. It can be retained at the exact state until the Activity or Fragment finishes.

在您的情况下,我想提供一个如何准备 ViewModel 的示例。让我们考虑您的 ViewModel GameModel ,它会保存您的电路板的布局项目。

In your case, I would like to provide an example of how you can prepare a ViewModel. Let us consider your ViewModel is GameModel which saves the layout items of your board.

public class GameModel extends ViewModel {
     public final LiveData<Game> gameLiveData = new LiveData<>();

     public GameModel() {
         // trigger game load.
     }

     void doAction() {
         // depending on the action, do necessary business logic calls and update the gameLiveData.
     }
}

public class Game {
    public static final int CROSS = 1; 
    public static final int ZERO = 0;

    public int pos1 = -1; // Default values are -1, when the position is yet to be played
    public int pos2 = -1;
    public int pos3 = -1;
    public int pos4 = -1;
    public int pos5 = -1;
    public int pos6 = -1;
    public int pos7 = -1;
    public int pos8 = -1;
    public int pos9 = -1;
}

现在来自活动,您需要在 GameModel 类中添加一个观察者来相应地更新UI。

Now from your Activity, you need to add an observer to your GameModel class to update the UI accordingly.

public class UserActivity extends Activity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.game_activity_layout);
         final GameModel viewModel = ViewModelProviders.of(this).get(GameModel.class);
         viewModel.gameLiveData.observer(this, new Observer() {
            @Override
             public void onChanged(@Nullable Game gameData) {
                 // update ui.
             }
         });
         findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                  // Pass parameters in doAction function to set the items in the Game class and update the ui accordingly inside the onChanged function.
                  viewModel.doAction(); 
             }
         });
     }
}

希望有所帮助!

这篇关于android旋转屏幕导致文本颜色更改为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆