Android中的评分(连续添加并保存HighScore的分数)

时间:2014-11-26 13:13:09

标签: java android

以下是我使用的代码。它的工作直到3次点击只有随后的点击不会像游戏中的正常评分一样“连续”添加。另一个问题是我不知道如何在时间结束后保存得分。我正在考虑使用数据(已保存的分数)在每场比赛结束时显示最佳分数。任何建议都将受到高度赞赏。

MainActivity.java

 //CALCULATE SCORE
    //declare a boolean variable for score
    private int optionTxtView = 0  ;
    private int addClick = 0  ;



    private void calculate(){
        x = Integer.parseInt(tv3.getText().toString().replaceAll("\\s",""));
        y = Integer.parseInt(tv2.getText().toString().replaceAll("\\s",""));
        z = x + y;
        score.setText(Integer.toString(z));
    }

    private void calculate2(){
        x = Integer.parseInt(score2.getText().toString().replaceAll("\\s",""));
        y = Integer.parseInt(tv2.getText().toString().replaceAll("\\s",""));
        z = x + y;
        score.setText(Integer.toString(z));
    }

    private void calculate3(){
        x = Integer.parseInt(score2.getText().toString().replaceAll("\\s",""));
        y = Integer.parseInt(score3.getText().toString().replaceAll("\\s",""));
        z = x + y;
        score.setText(Integer.toString(z));
    }

     //search 
    public void viewWord(View view)
    {   
        String s1= search.getText().toString();
        String s2= dbHelper.getData(s1);

        if(optionTxtView == 0){
        //display the score on textview1
            tv2.setText(s2);
            optionTxtView = 1;

            }
        else{
            if(optionTxtView == 1){
        //display the score on textview2    
            tv3.setText(s2);
            optionTxtView = 0;
            }
        }

        adapter.add(text.getText().toString());
        adapter.notifyDataSetChanged();
        text.clearComposingText();    

      //clicks = calculate to be use
        if(addClick == 0){
            calculate();
            score2.setText(score.getText());
            addClick = 1;
            text.clearComposingText();

        }
        else{
            if(addClick == 1){
                calculate();
                score2.setText(score.getText());
                addClick = 2;
                text.clearComposingText();

            }
            else{
                if(addClick == 2){
                    calculate2();
                    score2.setText(score.getText());
                    addClick = 3;
                    text.clearComposingText();
                }
                else{
                    if(addClick == 3){
                        calculate3();
                        score3.setText(score.getText());
                        addClick = 2;
                        text.clearComposingText();
                    }
            }

        }
    }

}

1 个答案:

答案 0 :(得分:1)

保存你可以使用SharedPrefrences的scoer:

保存你的分数: (gameData是以后用于获取sharedPrefrences的字符串)

SharedPreferences myData= getSharedPreferences("gameData", 0);
SharedPreferences.Editor editor = myData.edit();
editor.putInt("gameScore", myGameScore);
editor.commit;

加载你的分数:

SharedPreferences myData= getSharedPreferences("gameData", 0);
int myScore = myData.getInt(gameScore, 0);

之后," myScore"将包含您保存的分数(如果您之前未保存任何分数,则为0表示默认值。)

关于你问题的第一部分 - 我没有提到你所问的......