2017-01-08 39 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