所以,由于Displaymetrics,我有一个显示为 Popup 的活动,其中我有几个 CheckBoxes (checkBox1,checkBox2等)。
我希望在离开此活动时保存 已检查或未检查状态(以及部署更多操作:播放声音,附加字符串等),我这样做使用 SharedPreferences 。
到目前为止,工作代码
final checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox1.isChecked()) {
buttonSound.start();
} else {
buttonSound.start();
}
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("checkbox", checkBox1.isChecked());
editor.commit();
if (pref.getBoolean("checkbox", false) == true){
checkBox1.setChecked(true);
} else{
checkBox1.setChecked(false);
}
}
});
final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox2.setChecked(false);
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox2.isChecked()) {
buttonSound.start();
} else {
buttonSound.start();
}
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("checkbox", checkBox2.isChecked());
editor.commit();
if (pref.getBoolean("checkbox", false) == true){
checkBox2.setChecked(true);
} else{
checkBox2.setChecked(false);
}
}
}
只是完美!
然后,我在我的实际项目中上传了相同的代码,其中 Fragment 启动了checkBoxes所在的Popup活动。
BUT
当我在弹出区域外单击时(是的,我在前一个项目中也通过处理活动的样式做到了,这很好)并且我重新进入Popup活动,我看到状态不是已经保存了!
这两个这些项目都有相同的样式,复选框的ID,元素等等,它是纯粹的复制和粘贴,我认为这是活动的原因弹出活动之前是片段。
简而言之:片段 - >点击--->打开弹出活动--->我标记任何一个复选框 - >我点击外面或按回物理设备 - >我重新打开我不再检查弹出活动和我选中的复选框。
为什么这样?片段有什么变化,我应该如何调整我的代码?
编辑:
这是POPUP活动的原始代码
公共类Popup1扩展了Activity {
private TextView textViewResult;
private TextView textView2;
private TextView textView3;
private TextView textView4;
MediaPlayer buttonSound;
public String newString;
public String newString2;
public String newString3;
public String newString4;
public String risultato;
final ArrayList<String> builder = new ArrayList<String>();
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup3);
textViewResult = (TextView) findViewById(R.id.textViewResult);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);
textView4 = (TextView) findViewById(R.id.textView4);
buttonSound = MediaPlayer.create(Popup1.this, R.raw.scribble);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * 1.), (int) (height * .4));
final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox);
checkBox1.setChecked(false);
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox1.isChecked()) {
builder.add((String.valueOf(newString)));
builder.add("\n");
LayoutInflater inflater = getLayoutInflater();
View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Yippie, a custom toast");
Toast mytoast = new Toast(getBaseContext());
mytoast.setView(layouttoast);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
buttonSound.start();
} else {
builder.remove(String.valueOf(newString));
builder.remove("\n");
LayoutInflater inflater = getLayoutInflater();
View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item deselected");
Toast mytoast = new Toast(getBaseContext());
mytoast.setView(layouttoast);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
buttonSound.start();
}
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
SharedPreferences.Editor editor1 = pref.edit();
editor1.putBoolean("checkbox", checkBox1.isChecked());
editor1.commit();
if (pref.getBoolean("checkbox", false) == true) {
checkBox1.setChecked(true);
} else {
checkBox1.setChecked(false);
}
}
});
final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox2.setChecked(false);
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox2.isChecked()) {
builder.add((String.valueOf(newString2)));
builder.add("\n");
LayoutInflater inflater = getLayoutInflater();
View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Yippie, a custom toast");
Toast mytoast = new Toast(getBaseContext());
mytoast.setView(layouttoast);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
buttonSound.start();
} else {
builder.remove(String.valueOf(newString2));
builder.remove("\n");
LayoutInflater inflater = getLayoutInflater();
View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item deselected");
Toast mytoast = new Toast(getBaseContext());
mytoast.setView(layouttoast);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
buttonSound.start();
}
SharedPreferences pref2 = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
SharedPreferences.Editor editor2 = pref2.edit();
editor2.putBoolean("checkbox", checkBox2.isChecked());
editor2.commit();
if (pref2.getBoolean("checkbox", false) == true){
checkBox2.setChecked(true);
} else{
checkBox2.setChecked(false);
}
}
});
final CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3.setChecked(false);
checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox3.isChecked()) {
builder.add((String.valueOf(newString3)));
builder.add("\n");
LayoutInflater inflater = getLayoutInflater();
View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item selected");
Toast mytoast = new Toast(getBaseContext());
mytoast.setView(layouttoast);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
buttonSound.start();
} else {
builder.remove(String.valueOf(newString3));
builder.remove("\n");
LayoutInflater inflater = getLayoutInflater();
View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item deselected");
Toast mytoast = new Toast(getBaseContext());
mytoast.setView(layouttoast);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
buttonSound.start();
}
SharedPreferences pref3 = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
SharedPreferences.Editor editor3 = pref3.edit();
editor3.putBoolean("checkbox", checkBox3.isChecked());
editor3.commit();
if (pref3.getBoolean("checkbox", false) == true){
checkBox3.setChecked(true);
} else{
checkBox3.setChecked(false);
}
}
});
final CheckBox checkBox4 = (CheckBox) findViewById(R.id.checkBox2);
checkBox4.setChecked(false);
checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox4.isChecked()) {
builder.add((String.valueOf(newString4)));
builder.add("\n");
LayoutInflater inflater = getLayoutInflater();
View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item selected");
Toast mytoast = new Toast(getBaseContext());
mytoast.setView(layouttoast);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
buttonSound.start();
} else {
builder.remove(String.valueOf(newString4));
builder.remove("\n");
LayoutInflater inflater = getLayoutInflater();
View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item deselected");
Toast mytoast = new Toast(getBaseContext());
mytoast.setView(layouttoast);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
buttonSound.start();
}
SharedPreferences pref4 = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
SharedPreferences.Editor editor4 = pref4.edit();
editor4.putBoolean("checkbox", checkBox4.isChecked());
editor4.commit();
if (pref4.getBoolean("checkbox", false) == true){
checkBox4.setChecked(true);
} else{
checkBox4.setChecked(false);
}
}
});
final CheckBox checkBox5 = (CheckBox) findViewById(R.id.checkBox2);
checkBox5.setChecked(false);
checkBox5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox5.isChecked()) {
LayoutInflater inflater = getLayoutInflater();
View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item selected");
Toast mytoast = new Toast(getBaseContext());
mytoast.setView(layouttoast);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
buttonSound.start();
} else {
LayoutInflater inflater = getLayoutInflater();
View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item deselected");
Toast mytoast = new Toast(getBaseContext());
mytoast.setView(layouttoast);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
buttonSound.start();
}
SharedPreferences pref5 = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
SharedPreferences.Editor editor5 = pref5.edit();
editor5.putBoolean("checkbox", checkBox5.isChecked());
editor5.commit();
if (pref5.getBoolean("checkbox", false) == true){
checkBox5.setChecked(true);
} else{
checkBox5.setChecked(false);
}
}
});
final CheckBox checkBox6 = (CheckBox) findViewById(R.id.checkBox2);
checkBox6.setChecked(false);
checkBox6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox6.isChecked()) {
LayoutInflater inflater = getLayoutInflater();
View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item selected");
Toast mytoast = new Toast(getBaseContext());
mytoast.setView(layouttoast);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
buttonSound.start();
} else {
LayoutInflater inflater = getLayoutInflater();
View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item deselected");
Toast mytoast = new Toast(getBaseContext());
mytoast.setView(layouttoast);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
buttonSound.start();
}
SharedPreferences pref6 = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
SharedPreferences.Editor editor6 = pref6.edit();
editor6.putBoolean("checkbox", checkBox6.isChecked());
editor6.commit();
if (pref6.getBoolean("checkbox", false) == true){
checkBox6.setChecked(true);
} else{
checkBox6.setChecked(false);
}
}
});
getData();
}
private void getData() {
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
StringRequest stringRequest = new StringRequest(Config.DATA_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Popup1.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
StringRequest stringRequest2 = new StringRequest(Config.DATA_URL2, new Response.Listener<String>() {
@Override
public void onResponse(String response2) {
loading.dismiss();
showJSON2(response2);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Popup1.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue2 = Volley.newRequestQueue(this);
requestQueue2.add(stringRequest2);
StringRequest stringRequest3 = new StringRequest(Config.DATA_URL3, new Response.Listener<String>() {
@Override
public void onResponse(String response3) {
loading.dismiss();
showJSON3(response3);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Popup1.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue3 = Volley.newRequestQueue(this);
requestQueue3.add(stringRequest3);
StringRequest stringRequest4 = new StringRequest(Config.DATA_URL4, new Response.Listener<String>() {
@Override
public void onResponse(String response4) {
loading.dismiss();
showJSON4(response4);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Popup1.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue4 = Volley.newRequestQueue(this);
requestQueue4.add(stringRequest4);
}
private void showJSON(String response){
String ID="";
String Price="";
String WeightUnits = "";
String PWU = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
ID = collegeData.getString(Config.KEY_ID);
Price = collegeData.getString(Config.KEY_Price);
WeightUnits = collegeData.getString(Config.KEY_WeightUnits);
PWU = collegeData.getString(Config.KEY_PWU);
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setText("ID:\t" + ID + "\nPrice:\t" + Price + "\nWeight/Units:\t" + WeightUnits + "\nPrice per Weight/Units:\t" + PWU);
String newString= (String)("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units:\t"+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU).toString();
}
private void showJSON2(String response2){
String ID="";
String Price="";
String WeightUnits = "";
String PWU = "";
try {
JSONObject jsonObject = new JSONObject(response2);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY2);
JSONObject collegeData = result.getJSONObject(0);
ID = collegeData.getString(Config.KEY_ID);
Price = collegeData.getString(Config.KEY_Price);
WeightUnits = collegeData.getString(Config.KEY_WeightUnits);
PWU = collegeData.getString(Config.KEY_PWU);
} catch (JSONException e) {
e.printStackTrace();
}
textView2.setText("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units: "+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU);
String newString2= (String)("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units:\t"+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU).toString();
}
private void showJSON3(String response3){
String ID="";
String Price="";
String WeightUnits = "";
String PWU = "";
try {
JSONObject jsonObject = new JSONObject(response3);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY3);
JSONObject collegeData = result.getJSONObject(0);
ID = collegeData.getString(Config.KEY_ID);
Price = collegeData.getString(Config.KEY_Price);
WeightUnits = collegeData.getString(Config.KEY_WeightUnits);
PWU = collegeData.getString(Config.KEY_PWU);
} catch (JSONException e) {
e.printStackTrace();
}
textView3.setText("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units: "+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU);
String newString3= (String)("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units:\t"+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU).toString();
}
private void showJSON4(String response4){
String ID="";
String Price="";
String WeightUnits = "";
String PWU = "";
try {
JSONObject jsonObject = new JSONObject(response4);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY4);
JSONObject collegeData = result.getJSONObject(0);
ID = collegeData.getString(Config.KEY_ID);
Price = collegeData.getString(Config.KEY_Price);
WeightUnits = collegeData.getString(Config.KEY_WeightUnits);
PWU = collegeData.getString(Config.KEY_PWU);
} catch (JSONException e) {
e.printStackTrace();
}
textView4.setText("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units: "+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU);
String newString4= (String)("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units:\t"+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU).toString();
}
@Override
public void finish() {
String risultato = builder.toString().replace("[", "").replace("]", "");
// Prepare data intent
Intent returnIntent = new Intent();
returnIntent.putExtra("result", "papparapa");
setResult(RESULT_OK, returnIntent );
super.finish();
}
}