save the state of button in android

六眼飞鱼酱① 提交于 2020-01-26 01:14:44

问题


am working on camera application i have two imageview one is auto and second is pro i want when i click on auto auto is selected and image icon changes and when i click on pro automatically auto view deselected and pro view is selected

autobtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            autobtn.setImageResource(R.drawable.autoactive);
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ModeActivity.this);
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString("focus_value", "focus_mode_auto");
            Intent it = new Intent(ModeActivity.this, MainActivity.class);
            startActivity(it);
            edit.commit();
            //MainActivity.grid.setVisibility(View.VISIBLE);
        }
    });

in the second picture pro is selected


回答1:


You can use the SharedPreferences like this.

SharedPreferences sharedPreferences; 

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ModeActivity.this);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean("focus_value", false);
        edit.putBoolean("auto_value", false); 
        edit.commit();

In your button event,

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

        boolean auto = edit.getBoolean("auto_value", false);
        boolean pro = edit.getBoolean("pro_value", false);

        if(!auto){
            edit.putBoolean("auto_value", true);
            autobtn.setImageResource(R.drawable.autoactive);
            edit.putBoolean("pro_value",false);
            probtn.setImageResource(R.drawable.xxxxx);
        }

        Intent it = new Intent(ModeActivity.this, MainActivity.class);
        startActivity(it);

    }
});



回答2:


Your AUTO button

autobtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        autobtn.setBackgroundResource(R.drawable.autoactive);     //Change the AUTO image to selected
        proButton.setBackgroundResource(R.drawable.proinactive);  //Change image PRO to deselected
        SaveButtonState("focus_mode_auto");                       //Save the button state

        Intent it = new Intent(ModeActivity.this, MainActivity.class);
        startActivity(it);
    }
});

Your PRO button

proButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        autoButton.setBackgroundResource(R.drawable.autoinactive);   //Change the AUTO image to deselected
        proButton.setBackgroundResource(R.drawable.proactive);       //Change PRO image to selected
        SaveButtonState("focus_mode_pro");                           //Save the button state

        Intent it = new Intent(ModeActivity.this, MainActivity.class);
        startActivity(it);
    }
});

Save method

public void SaveButtonState(String bState){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ModeActivity.this);
    SharedPreferences.Editor edit = sharedPreferences.edit();
    edit.putString("focus_value", bState);
    edit.commit();
}

Load method

public String LoadButtonState(){  
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);   
    String buttonState = preferences.getString("focus_value", "DEFAULT");
    return buttonState;
}

Use LoadButtonState() in your onCreate() to retrieve the last states of the buttons when your application starts, something like:

in your onCreate() method

String buttonState = LoadButtonState();

if(buttonState.equals("focus_mode_auto"){
    aautobtn.setBackgroundResource(R.drawable.autoactive);      //Change the AUTO image to selected
    proButton.setBackgroundResource(R.drawable.proinactive);    //Change image PRO to deselected
}
else if(buttonState.equals("focus_mode_pro"){
    autoButton.setBackgroundResource(R.drawable.autoinactive);  //Change the AUTO image to deselected
    proButton.setBackgroundResource(R.drawable.proactive);      //Change PRO image to selected
}

Something like this.




回答3:


You are launching the activity before actually saving the value in SharedPreference, that's why you are not able to fetch the saved value in MainActivity (probably because you are trying to fetch the value in onCreate of MainActivity).

Just move

edit.commit();

before

Intent it = new Intent(ModeActivity.this, MainActivity.class);

Update Do this in onResume

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ModeActivity.this);
String selectedBtn = SharedPreferences.getString("focus_value", "nothing_selected");
if(selectedBtn.equlas("focus_mode_auto"))
    // Select the auto button
else 
    // select the pro button



回答4:


Yes you can save the status for button in Prefresences as you already use and change the state of ther on both the click listener handler for both button



来源:https://stackoverflow.com/questions/35553811/save-the-state-of-button-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!