【问题标题】:android checkbox preference getting and setting the boolean valuesandroid 复选框首选项获取和设置布尔值
【发布时间】:2014-04-06 19:52:10
【问题描述】:

我是 android 的初学者,我尝试制作一个带有偏好活动的简单手电筒应用程序。所以我的问题是;我想通过复选框使灯光和声音设置是可选的。

settings.xml

  <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"
       />

settings.java

public class Settings extends PreferenceActivity{

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);

    CheckBoxPreference pLight = (CheckBoxPreference)getPreferenceManager().findPreference("pref_light");

    pLight.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {

            boolean myValue = (Boolean) newValue;

            if(myValue){
            }

                    //NEED SOME HELP IN THERE


            return true;
        }
    });

我的turnOnFlash函数;

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

onStart 函数

@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 循环。并且不知道如何进行此实现。

【问题讨论】:

    标签: java android checkbox preference


    【解决方案1】:

    如果我理解正确,你应该得到偏好 onResume() 并采取相应的行动:

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

    【讨论】:

      【解决方案2】:

      我认为你应该使用;点击监听器

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-13
        • 1970-01-01
        • 2015-12-07
        • 1970-01-01
        相关资源
        最近更新 更多