Android 保存我的应用程序';永久删除设置
我有一个小程序(Android应用程序), 当我使用对话框方法更改ImageButton的图像时, 即使在关闭我的应用程序后,是否有办法保存这些更改/设置, 我试图使用SharedReferences,但我不知道如何做,是否存在任何解决方案来保存我的设置?? 谢谢 我发布我的代码:Android 保存我的应用程序';永久删除设置,android,application-settings,savechanges,permanent,Android,Application Settings,Savechanges,Permanent,我有一个小程序(Android应用程序), 当我使用对话框方法更改ImageButton的图像时, 即使在关闭我的应用程序后,是否有办法保存这些更改/设置, 我试图使用SharedReferences,但我不知道如何做,是否存在任何解决方案来保存我的设置?? 谢谢 我发布我的代码: import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private ImageButton buttonClick;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonClick = (ImageButton) findViewById(R.id.imageButton1);
buttonClick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("ChangeIcon");
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Choose the element concerned ");
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
image.setImageResource(R.drawable.default);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.buttonGas);
declineButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
buttonClick.setImageResource(R.drawable.first_possible_choice);
dialog.dismiss();
} });
Button secondo = (Button)dialog.findViewById(R.id.buttonFinestra);
secondo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){buttonClick.setImageResource(R.drawable.second_possible choice);
dialog.dismiss();
}
});
}
});
} }
正如您所说,最简单的方法是通过
SharedReferences
。不要期望这将是一个“永远的数据”,不过,用户可能会清理与你的应用程序相关的任何数据(例如,来自Android应用程序管理本身),你必须为这种情况做好准备
在本例中,您将看到如何在SharedReferences
中存储值:
SharedPreferences stordata = getSharedPreferences("a-string-that-identifies-your-app", Context.MODE_PRIVATE);
// Here you say you're going to edit the preferences
final SharedPreferences.Editor prefs = stordata.edit();
prefs.putString("my_planet", "Saturn");
// This line is important: it will store the changes
prefs.commit();
然后,一旦退出应用程序并希望恢复保存的首选项,只需拨打:
SharedPreferences stordata = getSharedPreferences("a-string-that-identifies-your-app", Context.MODE_PRIVATE);
final String storedPlanet = stordata.getString("my_planet", "");
----编辑---
这是一个关于如何处理
SharedReferences
的很好的例子,你可以找到Android的参考,看看你能做什么。正如你所说,最简单的方法是通过SharedReferences
。不要期望这将是一个“永远的数据”,不过,用户可能会清理与你的应用程序相关的任何数据(例如,来自Android应用程序管理本身),你必须为这种情况做好准备
在本例中,您将看到如何在SharedReferences
中存储值:
SharedPreferences stordata = getSharedPreferences("a-string-that-identifies-your-app", Context.MODE_PRIVATE);
// Here you say you're going to edit the preferences
final SharedPreferences.Editor prefs = stordata.edit();
prefs.putString("my_planet", "Saturn");
// This line is important: it will store the changes
prefs.commit();
然后,一旦退出应用程序并希望恢复保存的首选项,只需拨打:
SharedPreferences stordata = getSharedPreferences("a-string-that-identifies-your-app", Context.MODE_PRIVATE);
final String storedPlanet = stordata.getString("my_planet", "");
----编辑---
这是一个关于如何处理
SharedReferences
的很好的例子,你可以找到Android的参考,看看你能做什么。你应该使用SharedReferences来做这件事。
在onCreate集合中,上次选择的图像
private static final String PREFS_LAST_IMG = "prefs_last_img";
private SharedPreferences mPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPreferences = getPreferences();
buttonClick.setImageResource(mPreferences.getInt(PREFS_LAST_IMG, R.drawable.first_possible_choice)); // 2nd argument is default option, when nothing was selected before
}
然后,当用户选择图像时,只需保存它:
SharedPreferences.Editor editor = mPreferences.edit();
editor.putInt(PREFS_LAST_IMG, R.drawable.what_user_choose);
editor.commit();
您应该使用SharedReferences来实现这一点。 在onCreate集合中,上次选择的图像
private static final String PREFS_LAST_IMG = "prefs_last_img";
private SharedPreferences mPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPreferences = getPreferences();
buttonClick.setImageResource(mPreferences.getInt(PREFS_LAST_IMG, R.drawable.first_possible_choice)); // 2nd argument is default option, when nothing was selected before
}
然后,当用户选择图像时,只需保存它:
SharedPreferences.Editor editor = mPreferences.edit();
editor.putInt(PREFS_LAST_IMG, R.drawable.what_user_choose);
editor.commit();
即使在应用程序关闭后,也可以使用SharedReferences保存更改。
将资源映像路径保存到SharedReferences中的密钥。在图像加载时(调用onCreate()时)使用它
SharedReferences SharedReferences=GetSharedReferences(“名称”);
int imageResource=SharedReferences.getInt(“KEY_NAME”);
image.setImageResource(imageResource);
单击按钮,保存SharedReferences中的更改:
SharedPreferences.Editor spEditor = sharedPreferences.edit();
spEditor.putInt("KEY_NAME", <image resource>).commit();
SharedReferences.Editor spEditor=SharedReferences.edit();
spEditor.putInt(“KEY_NAME”),commit();
即使在应用程序关闭后,也可以使用SharedReferences保存更改。将资源映像路径保存到SharedReferences中的密钥。在图像加载时(调用onCreate()时)使用它
SharedReferences SharedReferences=GetSharedReferences(“名称”);
int imageResource=SharedReferences.getInt(“KEY_NAME”);
image.setImageResource(imageResource);
单击按钮,保存SharedReferences中的更改:
SharedPreferences.Editor spEditor = sharedPreferences.edit();
spEditor.putInt("KEY_NAME", <image resource>).commit();
SharedReferences.Editor spEditor=SharedReferences.edit();
spEditor.putInt(“KEY_NAME”),commit();
谢谢你!这个共享引用我不知道如何将它嵌入到我的代码中,我的模拟器告诉我error@ulyssessPax我已经用两个有用的链接更新了我的帖子,第一个是完整的一步一步的例子,我建议尝试用这种方式更好地理解它。谢谢!这个共享引用我不知道如何将它嵌入到我的代码中,我的模拟器告诉我error@ulyssessPax我已经用两个有用的链接更新了我的帖子,第一个是一个完整的分步示例,我建议尝试用这种方式来更好地理解它。如果可能的话,你能解释一下如何将它放在上下文/环境中!如果你有可能的话,你能解释一下如何将它放在上下文/环境中!