在另一个活动中使用首选项设置来设置按钮可见

5
我有一个问题,就是通过另一个活动来设置按钮的可见性。
代码解释:
首先是 menu.xml
<Button
    android:id="@+id/f1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/button1"
    android:visibility="visible" />

<ImageView
    android:id="@+id/f2lock"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:src="@drawable/levellocked"
    android:visibility="visible" />

<Button
    android:id="@+id/f2"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/button2"
    android:visibility="gone" />

"f2"按钮用于意图"leveltwo.class",但它仍然设置为隐藏,"f2lock"是用于锁定等级的ImageView。其次,是menu.java文件。
public class menu extends Activity {

Button f1, f2;
ImageView 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.famouslevel);
    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");
            startActivityForResult (level1, 0);              
        }             
    });     
}   

public void onActivityResult (int requestCode, int resultCode, Intent level1){
    super.onActivityResult (requestCode, resultCode, level1); 
    f2=(Button)findViewById(R.id.f2);      
    f2lock=(ImageView)findViewById(R.id.f2lock);

    switch (resultCode) {
        case 2:  f2.setVisibility(View.VISIBLE);
                 f2lock.setVisibility(View.GONE);            
    }      

    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");
            startActivityForResult (level2, 0);              
        }             
    });       
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.splashscreen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

使用以下代码来调用带有结果的levelone.java 因此,在levelone.java中,我将代码放置如下。
 public void onClick(View v){
                  setResult (2);
                  finish();          
                  }
                }); 

代码的功能是在level.class完成时,向menu.class发送结果(2)。
public void onActivityResult (int requestCode, int resultCode, Intent level1){
    super.onActivityResult (requestCode, resultCode, level1); 
    f2=(Button)findViewById(R.id.f2);      
    f2lock=(ImageView)findViewById(R.id.f2lock);

    switch (resultCode) {
        case 2:  f2.setVisibility(View.VISIBLE);
                 f2lock.setVisibility(View.GONE);            
    }      

上述代码是为了从levelone.class接收结果(2)并执行case 2函数。
问题是如何在case 2中使用和设置SharedPreferences?这样f2和f2lock的可见性就会被保存。
因为我已经尝试了SharedPreferences代码,但什么都没有发生,f2按钮仍然是GONE,f2lock ImageView仍然是VISIBLE。
我的意思是:
像游戏一样,当用户完成第1关时,第2关就会解锁。
但在这里,当第1关完成时,我让按钮可见。

嗨,我可以帮助你解决这个问题,但是我有点困惑你具体在问什么。可能是我太傻了,但是还是请你在评论中解释一下或者用 tl;dr 简要概括一下你的问题,这样我才能给你一个恰当的回答。谢谢。 - Sipty
我应该补充说,这将更加抽象化,与您的代码关联性更少。 - Sipty
我指的代码就像普通游戏一样,当第一关完成后,第二关就会解锁。 - RichFounders
是的,我想我明白了!现在正在撰写答案。 - Sipty
@Sipty兄弟,你能帮我解决这个问题吗?http://stackoverflow.com/questions/32025103/make-button-visible-with-staticclass-and-save-it - RichFounders
2个回答

1

请写出以下代码:

Intent intent = new Intent();
intent.putExtra("key", 2);
setResult(RESULT_OK, intent);
finish();

并检查活动结果:

public void onActivityResult (int requestCode, int resultCode, Intent level1) {
    super.onActivityResult(requestCode, resultCode, level1);
    if (requestCode == 0 && resultCode == RESULT_OK) {
        if(level1.getExtras() != null){
            int key = level1.getIntExtra("key",0);
            if(key == 2){
                f2.setVisibility(View.VISIBLE);
            f2lock.setVisibility(View.GONE);
            }
        }
    }
}

但是我该如何将偏好设置放入其中?这样按钮的可见性就会被保存吗? - RichFounders

1
希望我正确理解了您的问题。如果我没有理解,请纠正我!
我认为有几种不同的解决方案适用于您的用例:
第一种方法是一个适当的SharedPreferences实现,直接在您现有的代码之上进行构建。在我的眼中,这并不是最好的方法,因为它滥用了SharedPreferences的目的。
另一种做法是在不同的活动中实现回调,但是随着添加更多层次,这将变得乏味。
我的解决方案是拥有一个不同的类,其中包含静态值,用于存储玩家的进度。如果您希望进度在会话之间保持不变,则还可以将其发展为文件,然后将其写入磁盘。
您只需要使用一个简单的接口函数(例如getPlayerProgress())检查玩家的进度,每当您需要时,该函数就会返回一个整数,说明达到的最高级别是什么。这还假定您使用单独的函数处理界面,该函数将在每个级别/游戏开始等时调用。例如,该函数的名称为updateLevel()。这对您有意义吗?
以下是我提到的两个类的示例实现:
/**
 * A static class, which handles all player progress, throughout the lifespan of the app.
 *
 */
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:
            /*
             * Add the code, which would display level one. 
             * This would include all button visibilities and maybe load level resources ect.
             */
            break;
        case 2:
            /*
             * Same as above.
             */
            break;

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

如果我理解有误,请纠正我。如果您需要示例代码,请随时提出。

祝您拥有愉快的一天。


这个 getPlayerProgress() 就像是加载游戏中保存的最后数据吗? - RichFounders
它将访问你尝试使用共享首选项保存的信息,在这种情况下:玩家的进度。我忘了提到,我假设使用接口处理函数。让我为你编辑它。 - Sipty
当然可以,让我为您快速准备一下。 - Sipty
嗨,我有一个问题,Sipty。首先,我需要使用startActivityForResult(levels,0);和onActivityResult吗? - RichFounders
没有使用SharedPreferences,进度会被保存吗? - RichFounders
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接