当我设计我的设置页面以设置一些参数时,我遇到了以下问题:
修改:我添加了我的设置代码,如下所示: 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();
}
}
答案 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()