Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何保存按钮';是否在onSaveInstanceState中设置s isSelected()属性,然后使用onRestoreInstanceState将其还原?_Java_Android - Fatal编程技术网

Java 如何保存按钮';是否在onSaveInstanceState中设置s isSelected()属性,然后使用onRestoreInstanceState将其还原?

Java 如何保存按钮';是否在onSaveInstanceState中设置s isSelected()属性,然后使用onRestoreInstanceState将其还原?,java,android,Java,Android,我想在onSaveInstanceState中保存按钮的isSelected()属性,然后在旋转设备时使用onRestoreInstanceState将其还原。这是一个骰子游戏,我有三种骰子,在投掷活动骰子(白色),在选择任何骰子(红色),在选择和得分后停用的骰子是黑色 一旦我投掷、选择、得分并旋转手机,活动骰子将恢复,停用的骰子也可以恢复,但我想在旋转手机之前恢复我可能单击过的骰子(但没有得分,因为得分将使它们成为停用的黑色骰子) //Change the dice icon on selec

我想在onSaveInstanceState中保存按钮的isSelected()属性,然后在旋转设备时使用onRestoreInstanceState将其还原。这是一个骰子游戏,我有三种骰子,在投掷活动骰子(白色),在选择任何骰子(红色),在选择和得分后停用的骰子是黑色

一旦我投掷、选择、得分并旋转手机,活动骰子将恢复,停用的骰子也可以恢复,但我想在旋转手机之前恢复我可能单击过的骰子(但没有得分,因为得分将使它们成为停用的黑色骰子)

//Change the dice icon on selection
private void setSelectDice(ImageButton imgBtn, int face, boolean stat) {

    imgBtn.setSelected(stat);
    if (stat) {
        imgBtn.setImageResource(imgSelectedDice[face-1]);
    }  
}

//save instance
public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);

        savedInstanceState.putIntArray("diceValues", diceFaces);
        savedInstanceState.putInt("totalScore", diceAction.getTotalScore());

        //dice action is a handler class that has certain dice operation
        //this part is working fine where i want to restore the active dices that i have thrown
        List<Integer> activeDiceList = diceAction.getCurrentDice();
        int[] activeDice = new int[activeDiceList.size()];
        for (int i = 0; i < activeDiceList.size(); i++) {
            activeDice[i] = activeDiceList.get(i);
        }
        savedInstanceState.putIntArray("activeDice", activeDice);

        //Here i got stuck, I am trying to use the same techniques above to create a list  of selected dice
        ArrayList<Integer> selDiceList = new ArrayList<Integer>();
        int[] selDice = new int[selDiceList.size()];
        for (int i = 0; i < imgBtnDice.length; i++) {
            if (imgBtnDice[i].isSelected()) {
                selDiceList.add(i);
                selDice[i] = selDiceList.get(i);
            }
        }
        savedInstanceState.putIntArray("selectedDice", selDice);

    }


@Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Always call the superclass so it can restore the view hierarchy
        super.onRestoreInstanceState(savedInstanceState);

        diceFaces = savedInstanceState.getIntArray("diceValues");

        //Array of active dice, working fine
        int[] activeDice = savedInstanceState.getIntArray("activeDice");
        //Array of selected dice, ???
        int[] selDice = savedInstanceState.getIntArray("selectedDice");

        boolean isThrowableStat = savedInstanceState.getBoolean("throwStat");
        boolean isSavableStat = savedInstanceState.getBoolean("saveStat");
        diceAction = new DiceHandler(diceFaces, activeDice, isThrowableStat, isSavableStat);

        diceAction.setTotalScore(savedInstanceState.getInt("totalScore"));
        // Creating a list of current dice and traversing it to set up active dice image and deactivated dice image, working fine

        List<Integer> activeDiceList = diceAction.getCurrentDice();
        for (int i = 0; i < diceFaces.length; i++) {
            imgBtnDice[i].setImageResource(imgActiveDice[diceFaces[i] - 1]);    
            setDeactiveDice(imgBtnDice[i], diceFaces[i], activeDiceList.contains(i));

        }

        //trying to do the same technique as above but not working
        List<Integer> selectedDiceList = new ArrayList<Integer>();
        for (int i = 0; i < diceFaces.length; i++) {
            setSelectDice(imgBtnDice[i], diceFaces[i], selectedDiceList.contains(i));
        }

        tvRound.setText(diceAction.getRoundNr().toString());
        tvFinalScore.setText(diceAction.getTotalScore().toString());
        tvTurnScore.setText(diceAction.getRoundScore().toString());

    }
//在选择时更改骰子图标
私有void setSelectDice(ImageButton imgBtn、int面、布尔统计){
imgBtn.setSelected(stat);
如果(统计){
imgBtn.setImageResource(imgSelectedDice[face-1]);
}  
}
//保存实例
SaveInstanceState上的公共无效(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putIntArray(“diceValues”,diceFaces);
savedInstanceState.putInt(“totalScore”,diceAction.getTotalScore());
//dice action是具有特定dice操作的处理程序类
//这部分工作正常,我想恢复我抛出的活动骰子
List activeDiceList=diceAction.getCurrentDice();
int[]activeDice=newint[activeDiceList.size()];
对于(int i=0;i

非常感谢您的帮助。除了上面提到的以外,还有其他方法可以实现这一点吗?

这里有一些知识不足的问题,
onrestoreinnstancestate
方法在
onCreate
方法之后调用,因此您必须检查
onCreate
中的所有保存状态。请参阅下面的代码

onCreate(Bundle b){
.....
.....
if(b!=null){
.....
.....     
//paste here your onRestoreInstanceState code thats it and Remove onRestoreInstanceState method no need it.
}
}

就是这样…

您尝试过将android:configChanges节点添加到活动的清单xml中吗

android:configChanges="keyboardHidden|orientation|screenSize"

我在OnCreate()中已经有了这段代码//如果(savedInstanceState!=null){onRestoreInstanceState(savedInstanceState);}或者{runGreed();},请检查我们是否正在重新创建以前销毁的实例