设置 StaticClass 以将我当前的 Button 设置为 Visible with GUI Handler

Set StaticClass to save my current Button set to Visible with GUI Handler

我对我朋友提供的 StaticClass 代码感到困惑,该代码使用 GUI 处理程序来替代共享首选项之外的保存

我的 selectlevel.class

中有以下代码
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);                 
             }            
         });
    }
}

所以我的朋友给了带有 GUI 处理程序的 StaticClass 代码,使按钮在最新级别 finish(); 时可见并保存它

这是下面的代码

static class PlayerProgress {
    // We set progress to static, so it would be alive, no matter what activity you're in.
    private static int progress = 1;
     /**
     * Update the player's progress.
     * @param levelNumber: latest level number.
     */
    public static void updateProgress(int levelNumber) {
        progress = levelNumber;
    }

    /**
     * Get the player's progress.
     * @return the latest level number.
     */
    public static int getPlayerProgress() {
        return progress;
    }
}

/**
 * The gui handler would need to be called, every time you need to update the screen to the
 * appropriate level and it's assets. (Buttons, activities ect.)
 * 
 * I would implement a MainActivity, which would handle the different screens. 
 *
 */

class guiHandler {
    public void updateLevel() {
        int progress = PlayerProgress.getPlayerProgress();
        /*
         * Add your 
         */
        switch(progress) {
        case 1:
             f2.setVisibility(View.VISIBLE);
             f2lock.setVisibility(View.GONE);
            break;
        case 2:
             f3.setVisibility(View.VISIBLE);
             f3lock.setVisibility(View.GONE);
            break;

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

我只是很困惑如何将我的代码和我朋友提供的 StaticClass 代码结合起来以在最新级别完成时保存按钮可见性

谁能帮我写代码?

更新

我像这样修改了我的代码

   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.
    }
  }

并在我的 levelone.class

中使用它
button1.setOnClickListener(new View.OnClickListener() {

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

levelone.class 完成 f2 按钮时仍然 GONE,如何解决这个问题?

因此,您可以使用此代码

selectlevel.java

package com.example.myapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class selectlevel extends Activity {

    Button f1, f2, f1lock, f2lock;

    @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);
        f1lock = (Button) findViewById(R.id.f1lock);
        f1.setOnClickListener(new View.OnClickListener() {

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

        f2 = (Button) findViewById(R.id.f2);
        f2lock = (Button) 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.myapp", "com.example.myapp.leveltwo");
                startActivity(level2);
            }
        });

        updateLevels();
    }

    static class PlayerProgress {
        // We set progress to static, so it would be alive, no matter what activity you're in.
        private static int progress = 0;

        /**
         * Update the player's progress.
         *
         * @param levelNumber: latest level number.
         */
        public static void updateProgress(int levelNumber) {
            progress = levelNumber;
        }

        /**
         * Get the player's progress.
         *
         * @return the latest level number.
         */
        public static int getPlayerProgress() {
            return progress;
        }
    }

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

        switch (progress) {
            case 0:
                f1.setVisibility(View.VISIBLE);
                f1lock.setVisibility(View.GONE);
                f2.setVisibility(View.GONE);
                f2lock.setVisibility(View.VISIBLE);
                break;

            default:
                f1.setVisibility(View.VISIBLE);
                f1lock.setVisibility(View.GONE);
                f2.setVisibility(View.VISIBLE);
                f2lock.setVisibility(View.GONE);
                break;
        }
    }
}

selectlevel.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
    >

    <Button
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:id="@+id/f1"
        android:text="Level 1"/>

    <Button
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:id="@+id/f1lock"
        android:text="Level 1 disabled"/>

    <Button
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:id="@+id/f2"
        android:text="Level 2"/>

    <Button
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:id="@+id/f2lock"
        android:text="Level 2 disabled"/>
</LinearLayout>

并且在你的关卡Activity更新进度

levelone.java

package com.example.myapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class levelone extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.levelone);
        Button finishf1 = (Button) findViewById(R.id.finishf1);
        finishf1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectlevel.PlayerProgress.updateProgress(1);
                startActivity(new Intent(levelone.this, selectlevel.class));
            }
        });
    }
}

levelone.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
    >

    <Button
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:id="@+id/finishf1"
        android:text="Finish Level 1"/>

</LinearLayout>