簡體   English   中英

使用onclick按鈕僅顯示一次自定義對話框

[英]show custom dialog only once with onclick Button

我只想顯示一次自定義對話框; 首先,我有一個自定義對話框包含一個button OK當我點擊確定按鈕,然后重新打開軟件,我想隱藏的自定義對話框我嘗試使用SharedPreference

   SharedPreferences.Editor editor;
    SharedPreferences pref;
    @SuppressLint("CommitPrefEdits")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pref = getApplicationContext().getSharedPreferences("MyPref", 0);
        editor = pref.edit();
        editor.putBoolean("key_name", false);

/**/

 if (pref.getBoolean("key_name", true)) {
                users();
  }

private void users() {
        final Dialog myDialog = new Dialog(MainActivity.this);
        myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        assert myDialog.getWindow() != null;
        myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        myDialog.setCancelable(false);
        myDialog.setContentView(R.layout.users_artwork_dialog);
        Button okbtn = myDialog.findViewById(R.id.okbtn);

        okbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
                @SuppressLint("CommitPrefEdits") SharedPreferences.Editor editor = pref.edit();
                editor.putBoolean("key_name", false);
                myDialog.dismiss();
            }
        });
        myDialog.show();
    }

您應該執行editor.commit()editor.apply()以將更改保存到users()方法中的SharedPreference 那應該解決問題。

您應該使用commit()apply() SharedPreference編輯器。

void commit()
提交您的首選項更改,從此編輯器回到正在編輯的SharedPreferences對象。 這將自動執行所請求的修改,從而替換SharedPreferences中當前存在的任何內容。

void apply ()提交您的首選項更改,從此編輯器返回到正在編輯的SharedPreferences對象。 這將自動執行所請求的修改,從而替換SharedPreferences中當前存在的任何內容。

還要檢查修改后的代碼

    SharedPreferences.Editor editor;
    SharedPreferences pref;

    @SuppressLint("CommitPrefEdits")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pref = getApplicationContext().getSharedPreferences("MyPref", 0);
        editor = pref.edit();
        editor.putBoolean("key_name", true);
        //Add commit()
        editor.commit();

/**/

        if (pref.getBoolean("key_name", false)) {
            users();
        }
    }
    private void users() {
        final Dialog myDialog = new Dialog(MainActivity.this);
        myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        assert myDialog.getWindow() != null;
        myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        myDialog.setCancelable(false);
        myDialog.setContentView(R.layout.users_artwork_dialog);
        Button okbtn = myDialog.findViewById(R.id.okbtn);

        okbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
                @SuppressLint("CommitPrefEdits") SharedPreferences.Editor editor = pref.edit();
                editor.putBoolean("key_name", false);
                //Add commit()
                editor.commit();
                myDialog.dismiss();
            }
        });
        myDialog.show();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM