首页 > 解决方案 > 如何将我的高分存储在某个地方,以便我可以使用它们从商店购买?如何管理我的总分和高分

问题描述

这是我的游戏结束 java 代码。如何保存我的高分,以便在我重新启动活动时显示。我使用了 sharedprefrences ,但它不会在我再次播放时存储我的高分 Closed。如何修改我的代码以永久存储所有尝试?如何确定历史最高分?每次运行程序后,我希望它提醒我“历史最高分是多少”。.

 package com. example.myantkillergame;
    import androidx.appcompat.app.AppCompatActivity;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageButton;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class GAMEOVER extends AppCompatActivity {
    private ImageButton imgbtn;
     TextView  totalscore,indv1,indv2,indv3,indv4,indv5,indv6,indv7,indv8,higscore;
          @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_g_a_m_e_o_v_e_r);
    
    
    
            int value1 = getIntent().getExtras().getInt("score1");
            int value2 = getIntent().getExtras().getInt("score2");
            int value3 = getIntent().getExtras().getInt("score3");
            int value4 = getIntent().getExtras().getInt("score4")
            int value5 = getIntent().getExtras().getInt("score5");
            int value6 = getIntent().getExtras().getInt("score6");
            int value7 = getIntent().getExtras().getInt("score7");
            int value8 = getIntent().getExtras().getInt("score8");
            totalscore=(TextView)findViewById(R.id.scoretotal) ;
           int score_count = getIntent().getIntExtra("m",0);
           totalscore.setText(score_count + "");
    higscore=(TextView)findViewById(R.id.highscorelabel);
    SharedPreferences setting= getSharedPreferences("GAME DATA", Context.MODE_PRIVATE);
    int highscore=setting.getInt("HIGH_SCORE :",0);
    if (score_count > highscore){
        higscore.setText("HIGHSCORE :" + score_count);
        //save
        SharedPreferences.Editor editor=setting.edit();
        editor.putInt("HIGH_SCORE",score_count);
        editor.commit();
    
    }else{
        higscore.setText("HIGHSCORE :" + highscore);
    }
    
    
        indv1=(TextView)findViewById(R.id.antone) ;
           indv1.setText(value1+"");
            indv2=(TextView)findViewById(R.id.anttwo) ;
            indv2.setText(value2+"");
            indv3=findViewById(R.id.antthree) ;
            indv3.setText(value3+"");
            indv4=(TextView)findViewById(R.id.antfour) ;
            indv4.setText(value4+"");
            indv5=(TextView)findViewById(R.id.antfive) ;
            indv5.setText(value5+"");
            indv6=(TextView)findViewById(R.id.antsix) ;
            indv6.setText(value6+"");
            indv7=(TextView)findViewById(R.id.antseven) ;
            indv7.setText(value7+"");
            indv8=(TextView)findViewById(R.id.anteight) ;
            indv8.setText(value8+"");
            imgbtn= (ImageButton)findViewById(R.id.btnbackgameover);
            imgbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnback();
                }
            });
        }
        public void btnback(){
            Intent intent=new Intent(this,MainActivity.class);
            startActivity(intent);
        }
        
        public void try again(View view)
        {
            startActivity(new Intent(getApplicationContext(),Selection.class));
        }
    }
    

标签: javaandroid

解决方案


您可以使用Android Shared Preferences. 共享首选项保存在设备存储中的 app 文件夹中,并且只能由应用程序访问。

参考文档:https ://developer.android.com/reference/android/content/SharedPreferences

培训:https ://developer.android.com/training/data-storage/shared-preferences


推荐阅读