Android Studio在开始游戏后显示对话框

时间:2017-01-08 22:12:23

标签: android dialog

我正在编程游戏,并且在按钮“开始游戏”之前表示将出现一个单独的对话框,其中解释了游戏。我仍然可以自己得到它!但我希望每个播放器的这个对话只出现一次,之后再也不会出现!有谁知道一种方法?

2 个答案:

答案 0 :(得分:0)

查看SharedPreferences

E.g:

{{1}}

答案 1 :(得分:0)

借助@Isaac Payne提供的代码,我正在修改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();
    }
}