Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在退出活动后保持多个复选框的选中状态?_Java_Android_Android Activity_Checked_Android Checkbox - Fatal编程技术网

Java 如何在退出活动后保持多个复选框的选中状态?

Java 如何在退出活动后保持多个复选框的选中状态?,java,android,android-activity,checked,android-checkbox,Java,Android,Android Activity,Checked,Android Checkbox,我真的需要一些帮助。即使在搜索了数小时并阅读了有关SharedReferences的主题后,我仍然无法解决我的问题 我希望即使我离开活动,复选框(多个复选框!)仍保持选中/未选中状态。如果我回到原始活动,复选框的状态应该与以前相同。 因此,您基本上可以将应用程序置于后台,如果您再次将其置于前台或转到其他活动,则仍应选中/取消选中复选框(取决于用户选中的内容) 下面是我的Activity.java代码: public class TennisActivity extends AppCompatAc

我真的需要一些帮助。即使在搜索了数小时并阅读了有关
SharedReferences
的主题后,我仍然无法解决我的问题

我希望即使我离开活动,复选框(多个复选框!)仍保持选中/未选中状态。如果我回到原始活动,复选框的状态应该与以前相同。 因此,您基本上可以将应用程序置于后台,如果您再次将其置于前台或转到其他活动,则仍应选中/取消选中复选框(取决于用户选中的内容)

下面是我的Activity.java代码:

public class TennisActivity extends AppCompatActivity {
    //Variabeln
    CheckBox cb118;
    CheckBox cb119;
    CheckBox cb120;
    CheckBox cb121;
    CheckBox cb122;
    CheckBox cb123;
    CheckBox cb149;
    CheckBox cb150;
    CheckBox cb151;
    CheckBox cb152;
    CheckBox cb153;

    //Going back to menu by pressing back on device
    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent homeIntent = new Intent(TennisActivity.this, SportActivity.class);
                startActivity(homeIntent);
                finish();
            }
        }, 1);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_tennis);

        //Variabeln initalisiern
        cb118 = (CheckBox) findViewById(R.id.checkBox118);
        cb119 = (CheckBox) findViewById(R.id.checkBox119);
        cb120 = (CheckBox) findViewById(R.id.checkBox120);
        cb121 = (CheckBox) findViewById(R.id.checkBox121);
        cb122 = (CheckBox) findViewById(R.id.checkBox122);
        cb123 = (CheckBox) findViewById(R.id.checkBox123);
        cb149 = (CheckBox) findViewById(R.id.checkBox149);
        cb150 = (CheckBox) findViewById(R.id.checkBox150);
        cb151 = (CheckBox) findViewById(R.id.checkBox151);
        cb152 = (CheckBox) findViewById(R.id.checkBox152);
        cb153 = (CheckBox) findViewById(R.id.checkBox153);
    }
}
提前感谢您的帮助

用于在用户切换复选框时保存当前状态。再次进入
onCreate
后,您可以从中重新加载数据

要获取共享首选项,请执行以下操作:

SharedPreferences sharedPref = 
getActivity().getSharedPreferences("MY_SHARED_APPLICATIONS_NAME", Context.MODE_PRIVATE);
写:

SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("MY_BOOL_VARIABLE_KEY", myBoolVariable);
editor.commit();
全文如下:

myBoolVariable = sharedPref.getBoolean("MY_BOOL_VARIABLE_KEY", defaultValue);
对于您的特定情况,您需要一个键来标识每个复选框,如下所示:

private static final String cb118Key = "cb118_key";
然后,在初始化复选框后,应根据
共享引用中保存的内容设置其状态:

cb118Checked = sharedPref.getBoolean(cb118Key, defaultValue);
cb118.setChecked(cb118Key);
唯一缺少的是在用户更改复选框状态时保存新状态:

cb118.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        editor.putBoolean(cb118Key, isChecked);
        editor.commit();
    }
});
用于在用户切换复选框时保存当前状态。再次进入
onCreate
后,您可以从中重新加载数据

要获取共享首选项,请执行以下操作:

SharedPreferences sharedPref = 
getActivity().getSharedPreferences("MY_SHARED_APPLICATIONS_NAME", Context.MODE_PRIVATE);
写:

SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("MY_BOOL_VARIABLE_KEY", myBoolVariable);
editor.commit();
全文如下:

myBoolVariable = sharedPref.getBoolean("MY_BOOL_VARIABLE_KEY", defaultValue);
对于您的特定情况,您需要一个键来标识每个复选框,如下所示:

private static final String cb118Key = "cb118_key";
然后,在初始化复选框后,应根据
共享引用中保存的内容设置其状态:

cb118Checked = sharedPref.getBoolean(cb118Key, defaultValue);
cb118.setChecked(cb118Key);
唯一缺少的是在用户更改复选框状态时保存新状态:

cb118.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        editor.putBoolean(cb118Key, isChecked);
        editor.commit();
    }
});

在共享首选项和检查值中存储标志。如果为真,则按下面的代码进行设置检查

String notif = pref.getString("notification", null);
if (notif != null && notif.equalsIgnoreCase("true")){
                checkBoxNotification.setChecked(true);

            }

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (checkBoxn.isChecked()) {
                        notification = "true";
                        editor.putString("notification", notification).commit();

                    } else {
                        notification = "false";
                        editor.putString("notification", notification).commit();
                    }
                }
            });

在共享首选项和检查值中存储标志。如果为真,则按下面的代码进行设置检查

String notif = pref.getString("notification", null);
if (notif != null && notif.equalsIgnoreCase("true")){
                checkBoxNotification.setChecked(true);

            }

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (checkBoxn.isChecked()) {
                        notification = "true";
                        editor.putString("notification", notification).commit();

                    } else {
                        notification = "false";
                        editor.putString("notification", notification).commit();
                    }
                }
            });

多亏了Natan,以下是解决方案: (请注意,必须添加一些导入到(通常是自动添加的),例如java.util.HashMap)

//Variabeln
复选框cb118;
复选框cb119;
复选框cb120;
复选框cb121;
复选框cb122;
复选框cb123;
复选框cb149;
复选框cb150;
复选框cb151;
复选框cb152;
复选框cb153;
//试一试
布尔myBoolVariable=false;
私有静态最终字符串cb118Key=“cb118_key”;
私有静态最终字符串cb119Key=“cb119_key”;
私有静态最终字符串cb120Key=“cb120_key”;
私有静态最终字符串cb121Key=“cb121_key”;
私有静态最终字符串cb122Key=“cb122_key”;
私有静态最终字符串cb123Key=“cb123_key”;
私有静态最终字符串cb149Key=“cb149_key”;
私有静态最终字符串cb150Key=“cb150_key”;
私有静态最终字符串cb151Key=“cb151_key”;
私有静态最终字符串cb152Key=“cb152_key”;
私有静态最终字符串cb153Key=“cb153\u key”;
SharedPreferences sharedPref=null;
//祖鲁克族体育活动
@凌驾
public void onBackPressed(){
//super.onBackPressed();
new Handler().postDelayed(new Runnable()){
@凌驾
公开募捐{
意向homeIntent=新意向(TennisActivity.this、SportActivity.class);
startActivity(家庭意向);
完成();
}
}, 1);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,WindowManager.LayoutParams.FLAG_全屏);
setContentView(R.layout.activity_网球);
setContentView(R.layout.activity_main);
//初始变量
cb118=(复选框)findviewbyd(R.id.checkBox118);
cb119=(复选框)findviewbyd(R.id.checkBox119);
cb120=(复选框)findviewbyd(R.id.checkBox120);
cb121=(复选框)findviewbyd(R.id.checkBox121);
cb122=(复选框)findViewById(R.id.checkBox122);
cb123=(复选框)findviewbyd(R.id.checkBox123);
cb149=(复选框)findViewById(R.id.checkBox149);
cb150=(复选框)findviewbyd(R.id.checkBox150);
cb151=(复选框)findviewbyd(R.id.checkBox151);
cb152=(复选框)findviewbyd(R.id.checkBox152);
cb153=(复选框)findviewbyd(R.id.checkBox153);
sharedPref=GetSharedReferences(“alessionegrini.checkliste”,Context.MODE\u PRIVATE);
//我将把它转换成一个映射,这样就可以很容易地遍历这些值
Map checkboxMap=newhashmap();
checkboxMap.put(cb118键,cb118);
checkboxMap.put(cb119键,cb119);
checkboxMap.put(cb120Key,cb120);
checkboxMap.put(cb121键,cb121);
checkboxMap.put(cb122键,cb122);
checkboxMap.put(cb123键,cb123);
checkboxMap.put(cb149键,cb149);
checkboxMap.put(cb150键,cb150);
checkboxMap.put(cb151键,cb151);
checkboxMap.put(cb152键,cb152);
checkboxMap.put(cb153键,cb153);
loadInitialValues(复选框映射);
setupCheckedChangeListener(checkboxMap);
}
public void loadInitialValues(映射checkboxMap){
对于(Map.Entry checkboxEntry:checkboxMap.entrySet()){
Boolean checked=sharedPref.getBoolean(checkboxEntry.getKey(),false);
checkboxEntry.getValue().setChecked(已选中);
}
}
public void setupCheckedChangeListener(映射checkboxMap){
对于(最终Map.Entry checkboxEntry:checkboxMap.entrySet()){
checkboxEntry.getValue().setOnCheckedChangeListener(新的CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
最终SharedReferences.Editor=SharedReference.edit();
putBoolean(checkboxEntry.getKey(),isChecked);
editor.apply();
}
});
}
}}

多亏了Natan,以下是解决方案: (请注意,有一些导入到——通常是au)