2

我有一个小程序(Android APP),当我使用 Dialog 方法更改 ImageButton 的图像时,即使在关闭我的应用程序后,有没有办法保存这些更改/设置,我尝试使用 SharedPreferences,但我不明白如何做,存在任何解决方案来保存我的设置?谢谢!

我发布我的代码:

 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();
                }

            });
        }
    });
} }
4

3 回答 3

2

即使在您的应用程序关闭后,也可以使用 SharedPreferences 来保留更改。
将资源图像路径保存到 SharedPrefernces 中的某个键。在图像加载时使用它(当 onCreate() 被调用时)。

SharedPreferences sharedPreferences = getSharedPreferences("Name", <Mode>);
int imageResource = sharedPreferences.getInt("KEY_NAME", <default value>);
image.setImageResource(imageResource);

单击按钮时,将更改保存在 SharedPreferences 中:

SharedPreferences.Editor spEditor = sharedPreferences.edit();
spEditor.putInt("KEY_NAME", <image resource>).commit();
于 2014-03-04T16:22:58.840 回答
1

正如您所说,最简单的方法是通过SharedPreferences. 不要指望这将是“永久数据”,但是,用户可能会清除与您的应用程序相关的任何数据(例如,来自 Android 应用程序管理本身),您必须为这种情况做好准备。

在这个例子中,您将看到如何在您的 : 中存储一个值SharedPreferences

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", "");

- - 编辑 - -

是一个关于如何处理的好例子SharedPreferences在这里你会找到 Android 的参考,看看你能做什么。

于 2014-03-04T16:10:27.390 回答
1

您应该使用 SharedPreferences 来做到这一点。 http://developer.android.com/guide/topics/data/data-storage.html

在你的 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();
于 2014-03-04T16:18:11.837 回答