保存android中的按钮状态

时间:2016-02-22 12:33:30

标签: android android-layout android-intent sharedpreferences

enter image description here

正在开发相机应用我有两个imageview一个是自动,第二个是我想要的当我点击自动自动选择和图像图标更改,当我点击自动自动自动查看取消选择和专业观点被选中

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);
        }
    });

enter image description here

选择第二张图片中的

4 个答案:

答案 0 :(得分:1)

您的AUTO按钮

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);
    }
});

您的PRO按钮

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);
    }
});

保存方法

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

加载方法

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

LoadButtonState()中使用onCreate()来检索应用程序启动时按钮的最后状态,例如:

你的onCreate()方法中的

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
}

像这样。

答案 1 :(得分:0)

是的,您可以在已经使用的情况下保存Prefresences中按钮的状态,并在两个按钮的单击侦听器处理程序上更改其状态

答案 2 :(得分:0)

您在实际保存SharedPreference中的值之前启动了活动,这就是为什么您无法在MainActivity中获取已保存的值(可能是因为您尝试获取onCreateMainActivity的值。

移动

edit.commit();

之前

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

更新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

答案 3 :(得分:0)

您可以像这样使用SharedPreferences。

SharedPreferences sharedPreferences; 

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

在您的按钮事件中,

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);

    }
});