代码之家  ›  专栏  ›  技术社区  ›  ulyssessPax

永久保存我的应用程序设置

  •  2
  • ulyssessPax  · 技术社区  · 11 年前

    我有一个小程序(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();
                    }
    
                });
            }
        });
    } }
    
    3 回复  |  直到 11 年前
        1
  •  2
  •   Green goblin    11 年前

    即使在应用程序关闭后,也可以使用SharedPreferences保存更改。
    将资源映像路径保存到SharedPreferences中的键。在图像加载时使用它(当调用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();
    
        2
  •  1
  •   banannn    11 年前

    您应该使用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();
    
        3
  •  1
  •   nKn    11 年前

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

    在本例中,您将看到如何在 共享首选项 :

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

    ----编辑----

    Here 这是一个很好的例子 共享首选项 here 你会找到Android的参考,看看你能做什么。

    推荐文章