Java android复选框首选项获取和设置布尔值
我是android的初学者,我尝试制作一个带有偏好活动的简单手电筒应用程序。所以我的问题是,;我想使灯光和声音设置是可选的复选框 settings.xmlJava android复选框首选项获取和设置布尔值,java,android,checkbox,preference,Java,Android,Checkbox,Preference,我是android的初学者,我尝试制作一个带有偏好活动的简单手电筒应用程序。所以我的问题是,;我想使灯光和声音设置是可选的复选框 settings.xml <PreferenceCategory android:title="@string/sct_behaviour"> <CheckBoxPreference android:key="pref_light" android:title="@string/st_pref_l
<PreferenceCategory
android:title="@string/sct_behaviour">
<CheckBoxPreference
android:key="pref_light"
android:title="@string/st_pref_light"
android:summary="@string/ss_pref_light"
android:defaultValue="true"
/>
<CheckBoxPreference
android:key="pref_switch_sound"
android:title="@string/st_pref_sound"
android:summary="@string/ss_pref_sound"
android:defaultValue="true"
/>
<CheckBoxPreference
android:key="pref_notification_sound"
android:title="@string/st_pref_notification_sound"
android:summary="@string/ss_pref_notification_sound"
android:defaultValue="true"
/>
我的闪光灯功能
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
// play sound
playSound();
}
}
启动函数
@Override
protected void onStart() {
super.onStart();
if(VERBOSE) Log.v(TAG, "++ ON START ++");
// on starting the app get the camera params
getCamera();
turnOnFlash(value);
}
我想根据用户的选择将此真值或假值设置为onStart cycle。我不知道如何实现这一点 如果我理解正确,您应该在Resume()上获取首选项并相应地采取行动:
protected void onResume() {
super.onResume();
if(VERBOSE) Log.v(TAG, "++ ON START ++");
// on starting the app get the camera params
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean lightPref = prefs.getBoolean("pref_light", true);
getCamera();
turnOnFlash(lightPref);
}
我认为你应该使用;单击侦听器
Preference pLight = (Preference)findPreference("pref_light");
Preference pSwitchSound = (Preference)findPreference("pref_switch_sound");
Preference pNotificationSound = (Preference)findPreference("pref_notification_sound");
pLight.setOnPreferenceClickListener(this);
pSwitchSound.setOnPreferenceClickListener(this);
pNotificationSound.setOnPreferenceClickListener(this);
Preference pLight = (Preference)findPreference("pref_light");
Preference pSwitchSound = (Preference)findPreference("pref_switch_sound");
Preference pNotificationSound = (Preference)findPreference("pref_notification_sound");
pLight.setOnPreferenceClickListener(this);
pSwitchSound.setOnPreferenceClickListener(this);
pNotificationSound.setOnPreferenceClickListener(this);