Android: Using Switch Preference pre API level 14

后端 未结 5 1662
有刺的猬
有刺的猬 2020-12-13 19:04

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

5条回答
  •  甜味超标
    2020-12-13 20:09

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

提交回复
热议问题