如何将复选框数据传递给另一个 activity
how to pass checkbox data to another activity
如何将复选框数据从一个 activity 传递到另一个?如果复选框是checked/selected,它将以String形式从第一个activity到第二个activity传递数据。第二个 activity 将获取字符串并在 _Job_fx 上设置文本,但为什么在未选中复选框时会出现其他复选框字符串?我的数据绕过我的条件并在 _Job_fx 上设置文本。如何解决这个问题?
第一activity:
sharedPreferences = getSharedPreferences("Add_job_fx", MODE_PRIVATE);
_electric_item01 = (CheckBox) findViewById(R.id.electric_item01);
checkbox(_electric_item01);
_electric_item02 = (CheckBox) findViewById(R.id.electric_item02);
checkbox(_electric_item02);
_electric_item03 = (CheckBox) findViewById(R.id.electric_item03);
checkbox(_electric_item03);
_electric_item04 = (CheckBox) findViewById(R.id.electric_item04);
checkbox(_electric_item04);
private void checkbox(final CheckBox check) {
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (check.isSelected()) {
//edit shared preference (put data)
SharedPreferences.Editor editor = sharedPreferences.edit();
//put data in shared preference
editor.putString(check.getText().toString(), check.getText().toString());
//apply changes to shared preference
editor.apply();
}
else {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(check.getText().toString());
editor.clear();
editor.apply();
}
}
});
}
秒activity:
//shared preferences (Name, Mode)
sharedPreferences = getSharedPreferences("Add_job_fx", MODE_PRIVATE);
//get data from shared preference
s_electric_item01 = sharedPreferences.getString("Change lightbulb", "");
s_electric_item02 = sharedPreferences.getString("Change starter", "");
s_electric_item03 = sharedPreferences.getString("Change light set", "");
s_electric_item04 = sharedPreferences.getString("Change fan + regulator", "");
//pass data to container for view
_Job_fx.setText("Job: " + s_electric_item01 +"\n"+ s_electric_item02 + "\n"+ s_electric_item03);
我想你需要先检查复选框是否被选中,如果被选中
returns true ,并相应地获取您的复选框值并将其用于
共享首选项
private void checkbox(final CheckBox check) {
SharedPreferences sharedPreferences =
getSharedPreferences("Add_job_fx", MODE_PRIVATE); //add this line here
check.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) { // here check if the checkbox is checked
// or not if yes then retreive the value and push it into
// Sharedpreferences
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("put a key here ",
check.getText().toString());
editor.apply();
}
else {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("put your key to remove from SP , the same key
as
above");
editor.clear();
editor.apply();
}
}
});
我已经解决了,关于sharedpreferences的事情是即使应用程序关闭它也会自动保存数据。所以默认情况下,每次我在第一次尝试后转到第二个 activity 时,它会将前一个字符串保存到 _Job_fx.setText
。解决方案只是通过删除以前的字符串将默认值设置为空。
sharedPreferences = getSharedPreferences("Add_job_fx", MODE_PRIVATE);
_electric_item01 = (CheckBox) findViewById(R.id.electric_item01);
checkbox(_electric_item01);
_electric_item02 = (CheckBox) findViewById(R.id.electric_item02);
checkbox(_electric_item02);
_electric_item03 = (CheckBox) findViewById(R.id.electric_item03);
checkbox(_electric_item03);
_electric_item04 = (CheckBox) findViewById(R.id.electric_item04);
checkbox(_electric_item04);
private void checkbox(final CheckBox check) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(check.getText().toString());
editor.apply();
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (check.isChecked()) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(check.getText().toString(), check.getText().toString());
editor.apply();
} else if (!check.isChecked()) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(check.getText().toString());
editor.apply();
}
}
});
}
如何将复选框数据从一个 activity 传递到另一个?如果复选框是checked/selected,它将以String形式从第一个activity到第二个activity传递数据。第二个 activity 将获取字符串并在 _Job_fx 上设置文本,但为什么在未选中复选框时会出现其他复选框字符串?我的数据绕过我的条件并在 _Job_fx 上设置文本。如何解决这个问题?
第一activity:
sharedPreferences = getSharedPreferences("Add_job_fx", MODE_PRIVATE);
_electric_item01 = (CheckBox) findViewById(R.id.electric_item01);
checkbox(_electric_item01);
_electric_item02 = (CheckBox) findViewById(R.id.electric_item02);
checkbox(_electric_item02);
_electric_item03 = (CheckBox) findViewById(R.id.electric_item03);
checkbox(_electric_item03);
_electric_item04 = (CheckBox) findViewById(R.id.electric_item04);
checkbox(_electric_item04);
private void checkbox(final CheckBox check) {
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (check.isSelected()) {
//edit shared preference (put data)
SharedPreferences.Editor editor = sharedPreferences.edit();
//put data in shared preference
editor.putString(check.getText().toString(), check.getText().toString());
//apply changes to shared preference
editor.apply();
}
else {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(check.getText().toString());
editor.clear();
editor.apply();
}
}
});
}
秒activity:
//shared preferences (Name, Mode)
sharedPreferences = getSharedPreferences("Add_job_fx", MODE_PRIVATE);
//get data from shared preference
s_electric_item01 = sharedPreferences.getString("Change lightbulb", "");
s_electric_item02 = sharedPreferences.getString("Change starter", "");
s_electric_item03 = sharedPreferences.getString("Change light set", "");
s_electric_item04 = sharedPreferences.getString("Change fan + regulator", "");
//pass data to container for view
_Job_fx.setText("Job: " + s_electric_item01 +"\n"+ s_electric_item02 + "\n"+ s_electric_item03);
我想你需要先检查复选框是否被选中,如果被选中 returns true ,并相应地获取您的复选框值并将其用于 共享首选项
private void checkbox(final CheckBox check) {
SharedPreferences sharedPreferences =
getSharedPreferences("Add_job_fx", MODE_PRIVATE); //add this line here
check.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) { // here check if the checkbox is checked
// or not if yes then retreive the value and push it into
// Sharedpreferences
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("put a key here ",
check.getText().toString());
editor.apply();
}
else {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("put your key to remove from SP , the same key
as
above");
editor.clear();
editor.apply();
}
}
});
我已经解决了,关于sharedpreferences的事情是即使应用程序关闭它也会自动保存数据。所以默认情况下,每次我在第一次尝试后转到第二个 activity 时,它会将前一个字符串保存到 _Job_fx.setText
。解决方案只是通过删除以前的字符串将默认值设置为空。
sharedPreferences = getSharedPreferences("Add_job_fx", MODE_PRIVATE);
_electric_item01 = (CheckBox) findViewById(R.id.electric_item01);
checkbox(_electric_item01);
_electric_item02 = (CheckBox) findViewById(R.id.electric_item02);
checkbox(_electric_item02);
_electric_item03 = (CheckBox) findViewById(R.id.electric_item03);
checkbox(_electric_item03);
_electric_item04 = (CheckBox) findViewById(R.id.electric_item04);
checkbox(_electric_item04);
private void checkbox(final CheckBox check) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(check.getText().toString());
editor.apply();
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (check.isChecked()) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(check.getText().toString(), check.getText().toString());
editor.apply();
} else if (!check.isChecked()) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(check.getText().toString());
editor.apply();
}
}
});
}