Make button visible with StaticClass and Save it

空扰寡人 提交于 2019-12-11 03:37:04

问题


I still got confused with StaticClass code which given from my friend to alternative save besides Shared Preferences, already 3 days I tried learned the code and asked but there is still a little problem with a code

this is the latest following code in my selectlevel.class that i have perfected

public class selectlevel extends Activity {

    Button f1, f2, f3;
    ImageView f2lock, f3lock;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.selectlevel);


       f1=(Button)findViewById(R.id.f1);
       f1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v){
                    // TODO Auto-generated method stub
                    Intent level1 = new Intent ();
                    level1.setClassName ("com.example.game", "com.example.game.levelone");
                    startActivity (level1);              
                }             
            }); 

        f2=(Button)findViewById(R.id.f2);
        f2lock=(ImageView)findViewById(R.id.f2lock);
        f2.setOnClickListener(new View.OnClickListener() {

             @Override
             public void onClick(View v){
               // TODO Auto-generated method stub
                Intent level2 = new Intent ();
                level2.setClassName ("com.example.game", "com.example.game.leveltwo");
                startActivity (level2);              
                }                       
          });
            updateLevels();
    }

    static class PlayerProgress {

        private static int progress = 0;

        public static void updateProgress(int levelNumber) {
            progress = levelNumber;
        }  

        public static int getPlayerProgress() {
            return progress;
        }
    }

    public void updateLevels() {
        int progress = PlayerProgress.getPlayerProgress();


    switch(progress) {
        case 1:
            f2.setVisibility(View.VISIBLE);
            f2lock.setVisibility(View.GONE);
            break;
        case 2:

            break;

        // You can expand this to as many levels you'd like.
    }
  }

and i had use this in my levelone.class to send update progress to 1

button1.setOnClickListener(new View.OnClickListener() {

          public void onClick(View v){
              selectlevel.PlayerProgress.updateProgress(1);
              finish(); 

but when levelone.class finish, f2 button still GONE and f2lock still VISIBLE

nothing is change in selectlevel.class

i wonder it can be visible like this and still visible if the game re-open because the button visibility is saved

can anyone help me to fix a problem in my code? or give explain with another code as solution?


回答1:


try to call the updateLevels() function also in your onClick funtion like this:

button1.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v){
          selectlevel.PlayerProgress.updateProgress(1);
          selectlevel.PlayerProgress.updateLevels();
          finish(); 
}


来源:https://stackoverflow.com/questions/32025103/make-button-visible-with-staticclass-and-save-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!