MainActivity.onCreate中的按钮图像没有变化

时间:2018-02-26 14:28:46

标签: java android android-imagebutton

我有一个静音按钮,通过onclick将我的应用静音。当我返回MainActivity(按钮所在的位置)时,我使用savedInstanceState方法保持静音或取消静音应用程序(取决于用户退出活动时的配置)。如果单击按钮并且应用程序处于静音状态,我希望(当我返回MainActivity时)按钮显示为静音(我有静音和取消静音的不同图像)。

我的代码:

SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    int defaultValue = getResources().getInteger(R.integer.mutecounterkeydefault);
     mutecounter= sharedPref.getInt(getString(R.string.mutecounterkey), defaultValue);
    mutebb = (ImageButton) findViewById(R.id.muteb);
    if (mutecounter == 1) { 
        mutebb.setImageResource(R.mipmap.muteon);} //this if is not working

    mutebb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MediaPlayerPool.mute = !MediaPlayerPool.mute;
            if (mutecounter == 0) {
                mutebb.setImageResource(R.mipmap.muteon);
                mutecounter = 1;
            } else {
                mutebb.setImageResource(R.mipmap.muteoff);
                mutecounter = 0;
            }
        }
    });
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt(getString(R.string.mutecounterkey), mutecounter);
    editor.apply();

解决方案代码是

 SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    int defaultValue = getResources().getInteger(R.integer.mutecounterkeydefault);
    mutecounter= sharedPref.getInt(getString(R.string.mutecounterkey), defaultValue);
    mutebb = (ImageButton) findViewById(R.id.muteb);
    if (mutecounter == 1) {
        mutebb.setImageResource(R.mipmap.muteon);
        MediaPlayerPool.mute = true;
    }

    mutebb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            if (mutecounter == 0) {
                mutebb.setImageResource(R.mipmap.muteon);
                mutecounter = 1;
                MediaPlayerPool.mute = true;
                        editor.putInt(getString(R.string.mutecounterkey), mutecounter);
            } else {
                mutebb.setImageResource(R.mipmap.muteoff);
                mutecounter = 0;
                MediaPlayerPool.mute = false;
                        editor.putInt(getString(R.string.mutecounterkey), mutecounter);


            }
            editor.apply();


        }
    });

2 个答案:

答案 0 :(得分:1)

问题是您在调用muteCounter之前保存OnClickListener

您的代码会设置onClickListener,然后立即将muteCounter保存到mutecounter当时保存的值,在这种情况下每次都会0

您需要做的是:

SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);

int defaultValue = getResources().getInteger(R.integer.mutecounterkeydefault);    
int mutecounter = sharedPref.getInt(getString(R.string.mutecounterkey), defaultValue);

ImageButton mutebb = (ImageButton) findViewById(R.id.muteb);

if (mutecounter == 1) {
    mutebb.setImageResource(R.mipmap.muteon);
}

mutebb.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        MediaPlayerPool.mute = !MediaPlayerPool.mute;

        SharedPreferences.Editor editor = sharedPref.edit();

        if (mutecounter == 0) {
            mutebb.setImageResource(R.mipmap.muteon);
            mutecounter = 1;


            editor.putInt(getString(R.string.mutecounterkey), mutecounter);

        } else {
            mutebb.setImageResource(R.mipmap.muteoff);
            mutecounter = 0;

            editor.putInt(getString(R.string.mutecounterkey), mutecounter);
        }

        editor.apply();
    }
});

我们在SharedPreferences这里设置要保存到OnClickListener的值,以便在点击按钮时始终设置它。

这样,下次加载视图时,它将在SharedPreferences中具有新值。当前的方式是,您始终将默认值保存为SharedPreferences,而不是在按下按钮时更新它。

答案 1 :(得分:0)

这是因为onClick方法中的代码仅在单击按钮时执行。并且在创建活动时立即执行onClick方法之外的代码。所以onClick方法所做的更改不会反映在sharedPreference中,因为在调用onClick方法时,不会执行优先保存数据的代码。

解决方案:

将onClick方法中的“保存数据保存到SharedPreferences”逻辑就像这样。

SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);     int defaultValue = getResources()。getInteger(R.integer.mutecounterkeydefault);      mutecounter = sharedPref.getInt(getString(R.string.mutecounterkey),defaultValue);     mutebb =(ImageButton)findViewById(R.id.muteb);     if(mutecounter == 1){         mutebb.setImageResource(R.mipmap.muteon);} //这个如果不起作用

mutebb.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        MediaPlayerPool.mute = !MediaPlayerPool.mute;
        if (mutecounter == 0) {
            mutebb.setImageResource(R.mipmap.muteon);
            mutecounter = 1;
        } else {
            mutebb.setImageResource(R.mipmap.muteoff);
            mutecounter = 0;
        }
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt(getString(R.string.mutecounterkey), mutecounter);
        editor.apply();
    }
});