应用退出后保存按钮状态

时间:2016-07-26 17:45:15

标签: android image button sharedpreferences

我有一个名为six1的按钮..我想在点击时更改按钮的图像。之后,即使我退出该应用程序,我仍希望永远保留该图像

这是我的代码..我使用了偏好但没有任何作用

public class Twentyeightdayspackmenu extends AppCompatActivity {
    Button six1;
    boolean clicked = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_twentyeightdayspackmenu);
        six1 = (Button) findViewById(R.id.six1);
        SharedPreferences.Editor editor = getSharedPreferences("com.mypck.asd", MODE_PRIVATE).edit();
        editor.putBoolean("NameOfThingToSave", true);
        editor.commit();



    }

    public void testclickok (View v)
    {
        SharedPreferences.Editor editor = getSharedPreferences("com.mypck.asd", MODE_PRIVATE).edit();
        clicked=true;
        if (clicked)
        {
            six1.setBackgroundResource(R.drawable.day2);
            editor.putBoolean("NameOfThingToSave", true);
            Intent bmi_intent = new Intent(Twentyeightdayspackmenu.this,bmi_activity.class);
            startActivity(bmi_intent);
        }
        else
        {
            six1.setBackgroundResource(R.drawable.day1);
            editor.putBoolean("NameOfThingToSave", false);

        }
        editor.commit();

    }



}



Thanks in advance

0 个答案:

没有答案