2013-07-15 58 views
3

任何想法爲什麼PreferenceScreen中有多個SwitchPreferences會產生一個問題,如果您選擇任何一個方框會導致其他方框更改?在運行4.2.2的Nexus 4上進行測試時發生此問題,但在運行4.0.4的Galaxy S3上未進行測試。感謝您提供的任何幫助。Android SwitchPreference在4.2.2中無法正常工作

<?xml version="1.0" encoding="utf-8"?> 

<PreferenceCategory android:title="@string/description_photo_preference"> 

    <SwitchPreference 
      android:key="use_gallery" 
      android:title="@string/title_use_gallery_preference" 
      android:summaryOff="@string/summary_dont_use_gallery_as_photo_source" 
      android:summaryOn="@string/summary_use_gallery_as_photo_source" 
      android:defaultValue="true" 
    /> 

    <SwitchPreference 
      android:key="use_camera" 
      android:title="@string/title_use_camera_preference" 
      android:summaryOff="@string/summary_dont_use_camera_as_photo_source" 
      android:summaryOn="@string/summary_use_camera_as_photo_source" 
      android:defaultValue="true" 
    /> 
    <SwitchPreference 
      android:key="show_last_vin" 
      android:title="@string/pref_string" 
      android:summaryOff="@string/pref_display__false" 
      android:summaryOn="@string/pref_display_true" 
      android:defaultValue="true" 
    /> 
</PreferenceCategory> 
<PreferenceCategory android:title="@string/description_photo_quality_settings"> 
    <ListPreference 
     android:key="prefPhotoQuality" 
     android:entries="@array/photo_quality_settings" 
     android:summary="@string/pref_user_photo_quality_settings" 
     android:entryValues="@array/photo_quality_settings_values" 
     android:title="@string/description_photo_quality_settings" /> 
</PreferenceCategory> 

+1

可以確認我面臨這個問題上的Nexus 4.2.2 – AAP

+0

我有我的Nexus 7上運行4.2.2 – Eric

+0

這是HTC感覺與Android 4.0相同.3,同樣的問題。 – marczellm

回答

2

它看起來這是所報告的this SO post一個已知的問題。

有一個解決方法,它可以找到here

這裏的解決方法代碼:

public class CustomSwitchPreference extends SwitchPreference { 

    /** 
    * Construct a new SwitchPreference with the given style options. 
    * 
    * @param context The Context that will style this preference 
    * @param attrs Style attributes that differ from the default 
    * @param defStyle Theme attribute defining the default style options 
    */ 
    public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    /** 
    * Construct a new SwitchPreference with the given style options. 
    * 
    * @param context The Context that will style this preference 
    * @param attrs Style attributes that differ from the default 
    */ 
    public CustomSwitchPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    /** 
    * Construct a new SwitchPreference with default style options. 
    * 
    * @param context The Context that will style this preference 
    */ 
    public CustomSwitchPreference(Context context) { 
     super(context, null); 
    } 

    @Override 
    protected void onBindView(View view) { 
     // Clean listener before invoke SwitchPreference.onBindView 
     ViewGroup viewGroup= (ViewGroup)view; 
     clearListenerInViewGroup(viewGroup); 
     super.onBindView(view); 
    } 

    /** 
    * Clear listener in Switch for specify ViewGroup. 
    * 
    * @param viewGroup The ViewGroup that will need to clear the listener. 
    */ 
    private void clearListenerInViewGroup(ViewGroup viewGroup) { 
     if (null == viewGroup) { 
      return; 
     } 

     int count = viewGroup.getChildCount(); 
     for(int n = 0; n < count; ++n) { 
      View childView = viewGroup.getChildAt(n); 
      if(childView instanceof Switch) { 
       final Switch switchView = (Switch) childView; 
       switchView.setOnCheckedChangeListener(null); 
       return; 
      } else if (childView instanceof ViewGroup){ 
       ViewGroup childGroup = (ViewGroup)childView; 
       clearListenerInViewGroup(childGroup); 
      } 
     } 
    } 

}