【问题标题】:set button Visible in another acticty with Preferences setting设置按钮在具有首选项设置的另一个活动中可见
【发布时间】:2015-08-13 12:49:10
【问题描述】:

我在通过另一个 Activity 设置按钮可见性时遇到问题

代码说明:

首先,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,但仍设置为 GONE, 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为finish()时,将Result(2)发送给menu.class

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函数

问题是如何在案例 2 中使用和设置 SharedPreferences?所以 f2 和 f2lock 可见性将被保存

因为我尝试了 SharedPreferences 代码但没有任何反应,f2 按钮仍然消失,f2lock imageview 仍然可见

我的意思是这样的:

就像游戏一样,当用户完成第 1 关后,第 2 关就会解锁

但是在这里我让按钮在 1 级完成后可见

【问题讨论】:

  • 你好。我可以帮你解决这个问题,但有点困惑你到底在问什么。可能我是一只傻鹅,但仍然 - 请介意在评论中解释,或者对你的问题做一个 tl;dr,这样我就可以给你一个充分的回应,拜托。
  • 我应该添加更多的抽象和更少的代码引用。
  • 我的意思的代码就像一个普通的游戏兄弟,当关卡1完成时,关卡2被解锁
  • 是的,我想我明白了!现在写一个答案。
  • @Sipty 兄弟你能帮我吗? stackoverflow.com/questions/32025103/…

标签: java android android-intent sharedpreferences


【解决方案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. 
        }
    }
}

再次,如果我有误解,请纠正我。如果您想要示例代码,请尽管询问。

希望你有一个美好的一天。

【讨论】:

  • hmm 所以这个 getPlayerProgress() 就像加载游戏中保存的最后一个数据?
  • 它将访问您尝试使用共享偏好保存的信息,在本例中为:玩家进度。我忘了提,我假设使用接口处理函数。请允许我为您编辑它。
  • 当然可以,让我为你准备点东西。
  • 我更新了答案。是否足够清楚,可以向您描述我的建议?
  • 嗯,我有一个问题。首先,我需要使用 startActivityForResult (levels, 0);和 onActivityresult 与否?
【解决方案2】:

编写以下代码:

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);
            }
        }
    }
}

【讨论】:

  • 但是我如何将偏好设置在那里?所以按钮的可见性被保存了?
猜你喜欢
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
  • 2012-01-20
  • 2017-01-06
  • 2011-09-02
  • 2012-02-12
  • 2023-03-18
  • 2019-11-04
相关资源
最近更新 更多