gpt4 book ai didi

java - 选中复选框后仅显示一次对话框

转载 作者:行者123 更新时间:2023-12-02 06:43:31 25 4
gpt4 key购买 nike

我在 oncreate 中有一个带有复选框的对话框。如果选中此复选框,则下次打开应用程序时不会显示对话框。我已将复选框状态保存在共享首选项中,它会显示我是否已选中该复选框,但对话框在启动时仍打开..出了什么问题?

编辑

//对话框 查看 checkBoxView = View.inflate(this, R.layout.checkbox, null); CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox); checkBox.setChecked(getFromSP("checkBox"));

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

private void saveInSp(String key,boolean value){
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
switch(buttonView.getId()){
case R.id.checkbox:
saveInSp("checkBox",isChecked);

break;
}
}
});
checkBox.setText("Never show it");

SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);

boolean hasBeenChecked = preferences.getBoolean("checkbox", false);

if (!hasBeenChecked) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(" TITLE");
builder.setMessage(" blablabla ")
.setView(checkBoxView)
.setCancelable(false)

.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();

}

最佳答案

我愿意:

  • (非必要)从匿名 OnCheckedChangeListener 中删除 saveInSp 并将其添加到 Activity 中,例如
  • (非必要)您可以通过引用 MyActivity.this.saveInSp 从监听器调用 saveInSp
  • 然后,在初始化并显示警报之前...

    SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);

    boolean hasBeenChecked = preferences.getBoolean("checkBox", false);

    if (!hasBeenChecked) {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(" TITLEs");
    builder.setMessage(" blablabla ")
    .setView(checkBoxView)
    .setCancelable(false)

    .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
    dialog.cancel();
    }
    }).show();
    }

关于java - 选中复选框后仅显示一次对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18878980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com