使用SharedPreferences更改Button的背景

时间:2014-06-16 07:24:06

标签: android button sharedpreferences

我想在按下按钮时更改背景图像,我希望它永远保持变化..

此代码中的问题是,当我关闭应用程序时,按钮被按预期禁用,但背景未更改。 这是我的代码:

day2.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        public void onClick(View view) {
            boolean firstrun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
                    .getBoolean("day2", true);
            if  (firstrun) {
                day2.setEnabled(true);
                day2.setBackgroundResource(R.drawable.img1);
                Intent myIntent = new Intent(MainActivity.this, Day02.class);
                MainActivity.this.startActivity(myIntent);


                } else {
                    day2.setBackgroundResource(R.drawable.img);
                    day2.setEnabled(false);

                }

            myVib.vibrate(50);
        }
    });

4 个答案:

答案 0 :(得分:0)

如果您需要更改SharedPreferences值,则需要更改代码,如下所示:

public void onClick(View view) {
        boolean firstrun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
                .getBoolean("day2", true);
        if  (firstrun) {
            day2.setEnabled(true);
            day2.setBackgroundResource(R.drawable.img1);
            Intent myIntent = new Intent(MainActivity.this, Day02.class);
            MainActivity.this.startActivity(myIntent);
            //This line
            getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putBoolean("day2",false).commit();


            } else {
                day2.setBackgroundResource(R.drawable.img);
                day2.setEnabled(false);

            }

        myVib.vibrate(50);
    }

希望有所帮助

答案 1 :(得分:0)

在共享首选项`btnSave.setOnClickListener中添加您的布尔值,如给定代码(new View.OnClickListener(){

    public void onClick(View v) {
        prefs = getSharedPreferences(settingsTAG, 0);
        Editor editor = prefs.edit();

        editor.putBoolean("rb1", rb1.isChecked());
        editor.commit();

        finish();

    }
} );`

答案 2 :(得分:0)

首次单击时,您需要在每次单击按钮时检查首选项并将值保存到getSharedPreferences并设置背景,当用户单击按钮两次时,它将运行{{1}阻止并且不会再次改变背景。

<强>样品:

else

在初始化按钮

之后将其放在您的onclick之外
        public void onClick(View view) {

        if(PreferenceManager.getDefaultSharedPreferences(main_Activity.this).getString("day2", "").length() == 0)
        {
            //will only run once it will only work in first click of button
            PreferenceManager.getDefaultSharedPreferences(main_Activity.this).edit().putString("day2", "1").commit();

            day2.setBackgroundResource(R.drawable.dumbbell1); //set the background forever
            day2.setEnabled(false);
        }
        else
        {
            //will only work on second click of button and so on..
            //will run when you click button again
        }

    }

我的代码有效:

`Button day2 = (Button)findViewById(....);`

    if(PreferenceManager.getDefaultSharedPreferences(main_Activity.this).getString("day2", "").length() != 0)
        {
            day2.setBackgroundResource(R.drawable.dumbbell1); //set the background forever
            day2.setEnabled(false);
        }

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button b = (Button) findViewById(R.id.button1); if(PreferenceManager.getDefaultSharedPreferences( this).getString("day2", "").length() != 0) { b.setBackgroundResource(R.drawable.your_image); //set the background forever b.setEnabled(false); } b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(PreferenceManager.getDefaultSharedPreferences(MainActivity.this).getString("day2", "").length() == 0) { //will only run once it will only work in first click of button PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit().putString("day2", "1").commit(); b.setBackgroundResource(R.drawable.your_image); //set the background forever b.setEnabled(false); } } }); } } xml

activity_main

答案 3 :(得分:0)

首先在你的类或帮助器类中创建一个方法

    private static void setButtonPref(Context ctx, String clicked)
{
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    Editor editor = prefs.edit();
    editor.putString("clicked", clicked);
    editor.commit();
}

然后在按钮中单击

    public void onClick(View view) {
      setButtonPref(context, "yes");
   }

然后在activity

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());

然后检查pref是否已经

if (sharedPref .getString("clicked", "yes").equals("nope")){
    // it never been clicked
}else{
    // it has been clicked before do whatever you want with the background
}