2017-01-08 72 views
0

我目前正在編程一款遊戲,並且在按鈕「開始遊戲」之前表示會出現一個單獨的對話框,其中會解釋遊戲的位置。我仍然可以自己得到它!但是我想每個玩家只會出現一次這個對話框,之後再也不會出現!有人知道一種方法嗎?Android Studio在開始遊戲後顯示對話框

+0

您是否在使用任何種類的存儲器來保存玩家的數據(積分,得分等)? –

+0

是的,我有一個服務器,其中存儲了玩家的分數和遊戲名稱本身 – Jordie

+0

因此,您可以在其中存儲布爾值,比如「isFirstTime」,如果玩家第一次存儲true,則存儲爲true。然後存儲錯誤而不是。或者你可以使用SharedPreferences,因爲@Isaac在下面的答案中得到了解決。 –

回答

0

與@Isaac佩恩所提供的代碼的幫助下,我修改爲Dialog的代碼。

public class MyApp extends Application { 

    SharedPreferences mPrefs; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     Context mContext = this.getApplicationContext(); 
       //0 = mode private. only this app can read these preferences 
     mPrefs = mContext.getSharedPreferences("myAppPrefs", 0); 


     // the rest of your app initialization code goes here 
     if(getFirstRun()) { 

     final Dialog dialog = new Dialog(your_activity_context.this); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //if not title window required. 
     dialog.setContentView(your_layout_to_show_as_window); 

     // initialize your views from the layout    
     setRan(); 
     dialog.show(); 
     } 
    } 

    public boolean getFirstRun() { 
     return mPrefs.getBoolean("firstRun", true); 
    } 

    public void setRan() { 
     SharedPreferences.Editor edit = mPrefs.edit(); 
     edit.putBoolean("firstRun", false); 
     edit.commit(); 
    } 
} 
+0

非常感謝!但setRan方法從來沒有使用過,當我啓動應用程序的第二次是對話框在那裏.. – Jordie

+0

是的錯誤,把'setRan'後面'dialog.show();'。它會工作。必須upvote和接受,如果幫助... – W4R10CK

+0

看到我編輯的代碼。 @Jordie – W4R10CK

0

看一看SharedPreferences

E.g:

public class MyApp extends Application { 

    SharedPreferences mPrefs; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     Context mContext = this.getApplicationContext(); 
       //0 = mode private. only this app can read these preferences 
     mPrefs = mContext.getSharedPreferences("myAppPrefs", 0); 


     // the rest of your app initialization code goes here 
     if(getFirstRun()) { 
      //Show dialog 
     } 
    } 

    public boolean getFirstRun() { 
     return mPrefs.getBoolean("firstRun", true); 
    } 

    public void setRan() { 
     SharedPreferences.Editor edit = mPrefs.edit(); 
     edit.putBoolean("firstRun", false); 
     edit.commit(); 
    } 
} 
+0

我的意思是我按下按鈕「開始遊戲」,然後出現一個對話XML文件,我解釋遊戲。那就是它 – Jordie