Android: Using Switch Preference pre API level 14

2020-02-08 04:33发布

问题:

Pre-API Level 14 there is no switch preference. If I use preferences.xml to create my preference screen is there some way to distinguish between the API levels? So having a check box for old releases and a switch for API 14?

What would be the best way?

回答1:

If I use preferences.xml to create my preference screen is there some way to distinguish between the API levels? So having a check box for old releases and a switch for API 14?

Create a res/xml-v14/ directory that contains a preferences.xml that has your SwitchPreference. Create a res/xml/ directory that contains a preferences.xml file that replaces the SwitchPreference with a CheckBoxPreference. Android will load the right preferences.xml file edition based on the device version the app is running on.



回答2:

You could also use the android-switch-backport library which has a SwitchPreference which works on Android 2.1+.

https://github.com/BoD/android-switch-backport



回答3:

There is a workaround which i'm not sure how full it is, by wrapping which view to use in the CheckBoxPreference (might miss some functions, but in general use, it works) .

The workaround will use the CheckBoxPreference for pre-API-14 and the SwitchPreference for API 14 and above.

Here's the code:

public class SwitchPreference extends CheckBoxPreference
  {
  android.preference.SwitchPreference _switchPreference =null;

  public SwitchPreference(final Context context)
    {
    super(context);
    if(VERSION.SDK_INT>=VERSION_CODES.ICE_CREAM_SANDWICH)
      _switchPreference=new android.preference.SwitchPreference(context);
    }

  public SwitchPreference(final Context context,final AttributeSet attrs)
    {
    super(context,attrs);
    if(VERSION.SDK_INT>=VERSION_CODES.ICE_CREAM_SANDWICH)
      _switchPreference=new android.preference.SwitchPreference(context,attrs);
    }

  public SwitchPreference(final Context context,final AttributeSet attrs,final int defStyle)
    {
    super(context,attrs,defStyle);
    if(VERSION.SDK_INT>=VERSION_CODES.ICE_CREAM_SANDWICH)
      _switchPreference=new android.preference.SwitchPreference(context,attrs,defStyle);
    }

  @Override
  protected View onCreateView(final ViewGroup parent)
    {
    final View view;
    if(VERSION.SDK_INT>=VERSION_CODES.ICE_CREAM_SANDWICH)
      {
      view=_switchPreference.getView(null,parent);
      // set as checked the view and the view's children, each in case it extend from Checkable
      ViewUtil.setChecked(view,isChecked());
      // set as non-clickable the view and the view's children
      ViewUtil.setClickable(view,false);
      }
    else view=super.onCreateView(parent);
    return view;
    }


回答4:

you can use SwitchCompat:

<android.support.v7.widget.SwitchCompat
    android:id="@+id/switch_compat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:checked="true"
    android:textOff="OFF"
    android:textOn="ON"
    app:showText="false"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

on setOnCheckedChangeListener:

SwitchCompat switchCompat = (SwitchCompat)convertView.findViewById(R.id.switch_compat);
        switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    textView.setText("check");
                } else {
                    textView.setText("unCheck");
                }
            }
        });

i hope help you.



回答5:

Try this code:

public class SettingsActivity extends PreferenceActivity {

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_settings);
        PreferenceScreen rootScreen = getPreferenceManager()
                .createPreferenceScreen(this);
        setPreferenceScreen(rootScreen);
        Preference NotifCheck=null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            NotifCheck = new SwitchPreference(this);

        } else {
            NotifCheck = new CheckBoxPreference(this);

        }
        NotifCheck.setKey("ShowNotification");
        NotifCheck.setTitle(R.string.ShowNotification);
        NotifCheck.setEnabled(true);
        rootScreen.addPreference(NotifCheck);
        // Show the Up button in the action bar.
        setupActionBar();
    }
}