如何在不更改设置页面中的任何设置值的情况下转换为主要活动?

时间:2018-03-10 15:10:43

标签: java android

当我设计我的设置页面以设置一些参数时,我遇到了以下问题:

  1. 当我打开设置页面时,它会在图片1 中显示默认值:
  2. 然后我将所有按钮设置为“打开”并返回主活动,如图片2 中所示:
  3. 当我从主要活动返回设置页面时,它会返回默认值,如图片3 中所示: 我真正想要的是,当我在设置页面中设置所有参数时,当我从主活动返回时,设置页面应该与图片2相同(在设置页面之后),如果您对此有任何想法,我将非常感激问题,非常感谢:
  4. Picture 1

    Picture 2

    Picture 3

    修改:我添加了我的设置代码,如下所示:     public class设置扩展了AppCompatActivity {

    private SwitchButton acc, gra, lin, gyr, rot, mag, aur, aul, std, stc, scs, bat, act, bea;
    private static  boolean macceleration, mgravity, mlinear_acceleration, mgyroscope, mrotation, mmagnetometer,
            maudio_recorder, maudio_level, mstep_detector, mstep_counter, mscreen_status, mbattery, mactivity, mbeacon_sets;
    
    
    public static final Boolean[] bl = {macceleration, mgravity, mlinear_acceleration, mgyroscope, mrotation, mmagnetometer,
            maudio_recorder, maudio_level, mstep_detector, mstep_counter, mscreen_status, mbattery, mactivity, mbeacon_sets};
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
    
        ArrayList<SwitchButton> butt = new ArrayList<SwitchButton>();
    
        acc = (SwitchButton) findViewById(R.id.acceleration);
        gra = (SwitchButton) findViewById(R.id.gravity);
        lin = (SwitchButton) findViewById(R.id.linear_acceleration);
        gyr = (SwitchButton) findViewById(R.id.gyroscope);
        rot = (SwitchButton) findViewById(R.id.rotation);
        mag = (SwitchButton) findViewById(R.id.magnetometer);
        aur = (SwitchButton) findViewById(R.id.audio_recoder);
        aul = (SwitchButton) findViewById(R.id.audio_level);
        std = (SwitchButton) findViewById(R.id.step_detector);
        stc = (SwitchButton) findViewById(R.id.step_counter);
        scs = (SwitchButton) findViewById(R.id.screen_status);
        bat = (SwitchButton) findViewById(R.id.battery);
        act = (SwitchButton) findViewById(R.id.activity);
        bea = (SwitchButton) findViewById(R.id.beacon_sets);
    
        // 
        acc.setChecked(true);macceleration = true;
        gra.setChecked(true);mgravity = true;
        lin.setChecked(true);mlinear_acceleration = true;
        gyr.setChecked(true);mgyroscope = true;
        bea.setChecked(true);mbeacon_sets = true;
    
        final SwitchButton[] ar = {acc, gra, lin, gyr, rot, mag, aur, aul, std, stc, scs, bat, act, bea};
    
    
        for (int i = 0; i<ar.length;i++){
            final int j;
            j = i;
            ar[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean b) {
                    if (b) {
                        bl[j] = true;
                        // if selected yes, open this sensor
                    } else {
                        bl[j] = false;
                        // if selected no, close this sensor
                    }
                }
            });
        }
    
    }
    
    @Override
    protected void onStart() {
        super.onStart();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
    }
    }
    

1 个答案:

答案 0 :(得分:0)

您没有向我们展示您的代码,但我认为这就是您所需要的

在SharedPrefernces中保存值

// MY_PREFS_NAME - a static String variable like: 
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putBoolean(/*ButtonChecked boolean*/, "ButtonState");
 editor.apply();

从首选项中检索数据

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
boolean restoredBool = prefs.getBoolean("ButtonState", null); //Use this boolean to enable and disable button in OnCreate method()