2015-08-13 105 views
5

我已經通過其他活動設置按鈕可見性問題的另一個acticty可見設置按鈕與偏好設置

代碼解釋:

首先,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 is ImageView for levellocked

其次,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();   
        } 
       }); 

代碼功能是發送結果( 2)到menu.classlevel.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級的

+0

嘿,那裏。我可以幫助你解決這個問題,但是有點困惑你究竟在問什麼。這可能是我成爲一隻愚蠢的鵝,但仍然 - 請在意見中解釋,或者對你的問題進行解答,以便我能夠給予你適當的迴應。 – Sipty

+0

有了更多的抽象和更少的代碼引用,我應該補充一下。 – Sipty

+0

代碼我的意思是像一個正常的遊戲兄弟,當1級完成2級解鎖 – RichFounders

回答

1

希望我理解正確你的問題。請糾正我,如果我沒有!

我看事情的方式是,有幾種不同的解決方案,以你的使用情況:

第一個,會是一個合適的SharedPreferences實現,直接建立在現有代碼的頂部。在我看來,這不是最好的方法,因爲它濫用了SharedPreferences的觀點。

這樣做的另一種方法是將回調實現到不同的活動中,但只是添加更多級別時會變得單調乏味。

我對此的解決方案是將一個不同的類與靜態值,這將存儲播放器的進度。如果您希望在會話之間保留進度,這也可以演變爲您寫入磁盤的文件。

你只需要用一個簡單的接口函數(如getPlayerProgress())來檢查播放器的進度,如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. 
     } 
    } 
} 

再次,如果我有誤解,請大家指正。如果您想要示例代碼,請儘量問一下。

我希望你有美好的一天。

+0

嗯所以這getPlayerProgress()就像加載保存在遊戲中的最後數據? – RichFounders

+0

在這種情況下,它將訪問您嘗試使用共享偏好保存的信息:玩家進步。我忘了提及,我假設使用接口處理函數。允許我爲你編輯它。 – Sipty

+0

我已經編輯過,請看看。 – Sipty

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

確定兄弟我會嘗試 – RichFounders

+0

,但我如何把偏好在那裏?所以按鈕可見性被保存? – RichFounders