无法在图像点击中保存共享首选项?

时间:2014-03-04 14:22:19

标签: android sharedpreferences android-preferences

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ib1= (ImageButton) findViewById(R.id.imageButton1);
        getpref();
        ib1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                if(sound==true){
                    sound=false;
                    ib1.setImageResource(R.drawable.volume);
                }
                else{
                    sound=true;
                    ib1.setImageResource(R.drawable.vol);
                }
            }
        });
    };
    public void getpref() {
        SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
        Boolean sh_sound = prefs.getBoolean("key", true);
        sound=sh_sound;
        if(sh_sound==true){
            SharedPreferences pref = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
            Editor editor = pref.edit();
            editor.putBoolean("key", sh_sound);
            editor.commit();
            checkpref(sh_sound);
        }
        else{
            SharedPreferences pref = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
            Editor editor = pref.edit();
            editor.putBoolean("key", sh_sound);
            editor.commit();
            checkpref(sh_sound);
        }
    }
    public void checkpref(Boolean sh_sound) {
        if(sh_sound== true){
            ib1.setImageResource(R.drawable.vol);
        }
        else{
            ib1.setImageResource(R.drawable.volume);
        }
    }

我想在图片点击时保存首选项,当我重新打开活动时,之前选择的选项应该在那里,但我无法存储首选项,它会在我重新打开时显示默认图像。

默认视图

选择要存储首选项的图像后

重新开启活动

4 个答案:

答案 0 :(得分:1)

看起来您实际上并未尝试在Click事件中保存首选项。尝试更新您的点击监听器,如下所示:

    ib1.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            if(sound==true){
                sound=false;
                ib1.setImageResource(R.drawable.volume);
            }
            else{
                sound=true;
                ib1.setImageResource(R.drawable.vol);
            }

            SharedPreferences pref = getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
            Editor editor = pref.edit();
            editor.putBoolean("key", sound);
            editor.commit();
        }
    });

除此之外,您似乎正在保存您刚刚在getpref()内阅读的值,您可以像这样简化它:

public void getpref() {
    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    Boolean sh_sound = prefs.getBoolean("key", true);
    sound=sh_sound;       

    checkpref(sh_sound);        
}

答案 1 :(得分:1)

我没有测试代码,但你的逻辑错了。您没有在点击时保存首选项。

Boolean sound;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ib1= (ImageButton) findViewById(R.id.imageButton1);
    getpref();
    ib1.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            if(sound==true){
                sound = false;
                ib1.setImageResource(R.drawable.volume);
            } else{
                sound = true;
                ib1.setImageResource(R.drawable.vol);
            }
            setSoundChecked(sound);
        }
    });
};

void setSoundChecked(Boolean checked) {
    SharedPreferences pref = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    Editor editor = pref.edit();
    editor.putBoolean("key", checked);
    editor.commit();
}

public void getpref() {
    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    Boolean sh_sound = prefs.getBoolean("key", true);
    sound = sh_sound;
    checkpref(sh_sound);
}

public void checkpref(Boolean sh_sound) {
    if(sh_sound== true){
        ib1.setImageResource(R.drawable.vol);
    }
    else{
        ib1.setImageResource(R.drawable.volume);
    }
}

答案 2 :(得分:0)

同时在按钮getpref()上作为最后一个语句或在活动onClick上调用onPause/OnStop方法,以将sh_sound的最新值保存在SharedPreferences中:

@Override
public void onPause()
{
      // call getpref() here..
     getpref();
    super.onPause();

}

答案 3 :(得分:0)

您正在使用getprefs()方法写入您的首选项,该方法仅在Activity启动时调用。单击Button时应该写。