2014-03-04 72 views
1

我有一个小程序(Android应用程序), 当我改变的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(); 
       } 

      }); 
     } 
    }); 
} } 

回答

1

使用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(); 
+0

如果对你有可能你可以解释 - 我是如何将它放置在上下文/环境中的! – ulyssessPax

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

---- ----编辑

Here“SA在如何处理很好的例子和here你会发现Android的参考,看看你有能力做什么。

+0

感谢NKN! 此SharedPreferences我不知道如何将它嵌入到我的代码中 我的模拟器告诉我的错误 – ulyssessPax

+0

@ulyssessPax我已经用2个有用的链接更新了我的帖子,第一个是完整的分步示例,我建议试图通过这种方式更好地理解它。 – nKn

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