无法在自定义SwitchPreference中切换开关

时间:2015-04-26 09:34:37

标签: android switchpreference

我尝试在adroid中获得自定义切换首选项。这是我的代码

 switch_widget.xml
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/customswitch"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:focusable="false"
   android:clickable="false" 
   />
  preference xml file :
 <com.tnavitech.utils.CustomSwitch
       android:title="Hide app icon" 
       android:key="hideicon"
       android:defaultValue="false"
       android:disableDependentsState="true"
       android:widgetLayout="@layout/switch_widget" 
    />

然后我扩展“SwitchPreference”,并在“onBindView”类中,并在OnPreferenceChangeListener中处理开关状态:

public class CustomSwitch extends SwitchPreference {

    Switch swit;

    public CustomCheckbox(Context context, AttributeSet attrs) {
    super(context, attrs);

   }

@Override
protected void onBindView(final View rootView) {
    ViewGroup viewGroup= (ViewGroup)rootView;
    super.onBindView(rootView);

    if(swit==null){

        swit  = (Switch) rootView.findViewById(R.id.customswitch);
    }

    this.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference arg0, Object arg1) {

            if (arg1 instanceof Boolean) {
                Boolean enabled = (Boolean) arg1;
                if(enabled){
                    swit.setChecked(true); /// cant toggle switch here
                    Toast.makeText(getContext(), "on", Toast.LENGTH_SHORT).show();


                } else {
                    Toast.makeText(getContext(), "off", Toast.LENGTH_SHORT).show();

                    swit.setSelected(false);     ///cant toggle switch here
            }
            }



            return true;
        }
    });

}

在setOnPreferenceChangeListener中,我无法打开或关闭开关。我该怎么解决这个问题?谢谢!

3 个答案:

答案 0 :(得分:0)

我正在开发使用API​​ 15的设备上运行的应用,因此必须解决此问题。该解决方案将为您的SwitchPreference提供两种不同的布局,而不是处理切换切换的布局。 Switch需要是clickable = false否则你的SwitchPreference不会处理任何点击。

首先是'on state'开关布局,其中button使用drawable代表ON按钮:

<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_switch_item"
    android:layout_width="48dp"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:textIsSelectable="false"
    android:textSize="0sp"
    android:button="@drawable/ico_toggle_on" //HERE IS DIFFERENCE BETWEEN THESE LAYOUTS
    android:thumb="@android:color/transparent"
    android:track="@android:color/transparent"
    android:clickable="false"
    android:focusable="false"
    android:gravity="center_vertical" />

第二种布局是OFF状态:

<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_switch_item"
    android:layout_width="48dp"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:textIsSelectable="false"
    android:textSize="0sp"
    android:button="@drawable/ico_toggle_off" //HERE IS DIFFERENCE BETWEEN THESE LAYOUTS
    android:thumb="@android:color/transparent"
    android:track="@android:color/transparent"
    android:clickable="false"
    android:focusable="false"
    android:gravity="center_vertical" />

然后最后在SwitchPreference OnPreferenceChangeListener上设置的Activity或Fragment中切换这两个布局:

final SwitchPreference expertSettingsPref = (SwitchPreference) findPreference("expert_settings");
    if (expertSettingsPref != null) {
        expertSettingsPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                preference.setWidgetLayoutResource((Boolean)newValue ? R.layout.custom_switch_preference_on : R.layout.custom_switch_preference_off);
                return true;
            }
        });
    }

答案 1 :(得分:0)

在PreferenceFragmentCompat中将SwitchPreferenceCompat与androidX结合使用,即可根据您的要求进行以下操作。

public class SettingsPageFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);

        final SwitchPreferenceCompat switchPreference = (SwitchPreferenceCompat) findPreference("switch");
        if(switchPreference != null){
            switchPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    // get the incoming value
                    boolean isOn = (Boolean) newValue;

                    // get the incoming control
                    SwitchPreferenceCompat switchPreference = (SwitchPreferenceCompat) preference;

                    // set the incoming value to the incoming control
                    switchPreference.setChecked(isOn);

                    // do whatever else you wanted to when this event fires

                }
            });
        }
    }

我怀疑您的代码无法正常工作的原因是“ swit”可能超出了侦听器的范围。在我的示例中,它作为首选项首选项传递。

答案 2 :(得分:0)

SwitchPreference Listener with Custom Layout重复。

SwitchView的ID应该为@+id/custom_switch_item,而不是SwitchPreference或任何其他自定义ID。

View switchView = view.findViewById(AndroidResources.ANDROID_R_SWITCH_WIDGET); 类的源代码中,我们可以找到以下代码:

AndroidResources

static final int ANDROID_R_SWITCH_WIDGET = android.R.id.switch_widget; 中的

@android:id/switch_widget

因此只能正确找到id = <Switch xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/switch_widget" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" />

这是有关SwitchPreference的widgetLayout的演示:

{{1}}
相关问题