本文整理汇总了Java中android.preference.SwitchPreference类的典型用法代码示例。如果您正苦于以下问题:Java SwitchPreference类的具体用法?Java SwitchPreference怎么用?Java SwitchPreference使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SwitchPreference类属于android.preference包,在下文中一共展示了SwitchPreference类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: bindPreferenceSummaryToValue
import android.preference.SwitchPreference; //导入依赖的package包/类
/**
* Binds a preference's summary to its value. More specifically, when the
* preference's value is changed, its summary (line of text below the
* preference title) is updated to reflect the value. The summary is also
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* @see #sBindPreferenceSummaryToValueListener
*/
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
if (preference instanceof SwitchPreference) {
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getBoolean(preference.getKey(), false));
} else {
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
}
示例2: onCreate
import android.preference.SwitchPreference; //导入依赖的package包/类
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.document_mode_preferences);
getActivity().setTitle(R.string.tabs_and_apps_title);
mDocumentModeManager = DocumentModeManager.getInstance(getActivity());
mDocumentModeSwitch = (SwitchPreference) findPreference(PREF_DOCUMENT_MODE_SWITCH);
boolean isdocumentModeEnabled = !mDocumentModeManager.isOptedOutOfDocumentMode();
mDocumentModeSwitch.setChecked(isdocumentModeEnabled);
mDocumentModeSwitch.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if ((boolean) newValue == !mDocumentModeManager.isOptedOutOfDocumentMode()) {
return true;
}
createOptOutAlertDialog((boolean) newValue).show();
return true;
}
});
}
示例3: onCreate
import android.preference.SwitchPreference; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.prefs = getSharedPreferences(Common.PREFS, Context.MODE_WORLD_READABLE);
addPreferencesFromResource(R.xml.preference);
mSwith = (SwitchPreference) findPreference("enable_toast");
mStyle = (ListPreference) findPreference("style_key");
mAnimation = (ListPreference) findPreference("animation_key");
mEnable_icon = (CheckBoxPreference) findPreference("enable_icon");
mGithub = findPreference("viewsource");
mSwith.setOnPreferenceChangeListener(this);
mStyle.setOnPreferenceChangeListener(this);
mAnimation.setOnPreferenceChangeListener(this);
mEnable_icon.setOnPreferenceChangeListener(this);
mGithub.setOnPreferenceClickListener(this);
}
示例4: bindPreferenceSummaryToValue
import android.preference.SwitchPreference; //导入依赖的package包/类
/**
* Binds a preference's summary to its value. More specifically, when the
* preference's value is changed, its summary (line of text below the
* preference title) is updated to reflect the value. The summary is also
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* @see #sBindPreferenceSummaryToValueListener
*/
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference' current value.
if (preference instanceof ListPreference
|| preference instanceof EditTextPreference) {
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
} else if (preference instanceof SwitchPreference
|| preference instanceof CheckBoxPreference) {
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getBoolean(preference.getKey(),true));
}
}
示例5: onCreate
import android.preference.SwitchPreference; //导入依赖的package包/类
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_wireless_socket);
setHasOptionsMenu(true);
PreferenceScreen preferenceScreen = this.getPreferenceScreen();
PreferenceCategory preferenceCategory = new PreferenceCategory(preferenceScreen.getContext());
preferenceCategory.setTitle("Wireless sockets");
preferenceScreen.addPreference(preferenceCategory);
for (int index = 0; index < WirelessSocketService.getInstance().GetDataList().getSize(); index++) {
WirelessSocket wirelessSocket = WirelessSocketService.getInstance().GetDataList().getValue(index);
SwitchPreference preference = new SwitchPreference(preferenceScreen.getContext());
preference.setTitle(wirelessSocket.GetName());
preference.setKey(wirelessSocket.GetSettingsKey());
preference.setDefaultValue(false);
preferenceCategory.addPreference(preference);
bindPreferenceSummaryToValue(preference);
}
}
示例6: updateShowTouchesOptions
import android.preference.SwitchPreference; //导入依赖的package包/类
private void updateShowTouchesOptions() {
final SwitchPreference showTouchesPref = (SwitchPreference) findPreference("show_touches");
int enable = 0;
try {
enable = Settings.System.getInt(getContentResolver(), Constants.SETTINGS_SYSTEM_SHOW_TOUCHES);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
showTouchesPref.setChecked(enable == 1);
showTouchesPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Settings.System.putInt(getContentResolver(),
Constants.SETTINGS_SYSTEM_SHOW_TOUCHES,
newValue == Boolean.TRUE ? 1 : 0);
return true;
}
});
}
示例7: setupNightModeSwitch
import android.preference.SwitchPreference; //导入依赖的package包/类
private void setupNightModeSwitch() {
// 设置夜间模式
uiManager = (UiModeManager) getActivity().getSystemService(Context.UI_MODE_SERVICE);
nightModeSwitch = (SwitchPreference) getPreferenceManager().findPreference("night_mode_switch");
nightModeSwitch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean nextNightMode = !PreferenceHelper.isNightModeEnabled();
if (nextNightMode) {
uiManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
} else {
uiManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
}
return true;
}
});
}
示例8: addSwitchPreferenceBasedOnCheckBoxPreference
import android.preference.SwitchPreference; //导入依赖的package包/类
static void addSwitchPreferenceBasedOnCheckBoxPreference(final CheckBoxPreference checkBox,
final PreferenceGroup group) {
final SwitchPreference switchPref = new SwitchPreference(checkBox.getContext());
switchPref.setTitle(checkBox.getTitle());
switchPref.setKey(checkBox.getKey());
switchPref.setOrder(checkBox.getOrder());
switchPref.setPersistent(checkBox.isPersistent());
switchPref.setEnabled(checkBox.isEnabled());
switchPref.setChecked(checkBox.isChecked());
switchPref.setSummary(checkBox.getSummary());
switchPref.setSummaryOn(checkBox.getSummaryOn());
switchPref.setSummaryOff(checkBox.getSummaryOff());
switchPref.setSwitchTextOn(EMPTY_TEXT);
switchPref.setSwitchTextOff(EMPTY_TEXT);
group.addPreference(switchPref);
switchPref.setDependency(checkBox.getDependency());
}
示例9: onCreate
import android.preference.SwitchPreference; //导入依赖的package包/类
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_settings);
bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
mContext = getActivity();
mFanFouDB = FanFouDB.getInstance(mContext);
loginout = findPreference("login_out");
loginout.setOnPreferenceClickListener(this);
switchNotify = (SwitchPreference)findPreference("notification");
switchNotify.setOnPreferenceClickListener(this);
versionCode = findPreference("version_code");
versionCode.setSummary(Utils.getVersionCode());
clearData = findPreference("clear_data");
clearData.setOnPreferenceClickListener(this);
}
示例10: onCreate
import android.preference.SwitchPreference; //导入依赖的package包/类
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
cacheWithWifi = (SwitchPreference) findPreference("cache_with_wifi");
cacheWithWifi.setOnPreferenceChangeListener(this);
boolean checked = PreferenceManager.getBooleanValue(context, CACHE_KEY, true);
cacheWithWifi.setChecked(checked);
clearVideoCache = findPreference("clear_video_cache");
clearVideoCache.setOnPreferenceClickListener(this);
float videoFileSize = calculateCacheFileSize("video-cache", true);
clearVideoCache.setSummary(String.format(Locale.CHINESE, "%.2fMb", videoFileSize));
clearPhotoCache = findPreference("clear_photo_cache");
clearPhotoCache.setOnPreferenceClickListener(this);
float photoFileSize = calculateCacheFileSize("image_manager_disk_cache", false);
photoFileSize += calculateCacheFileSize("picasso-cache", false);
clearPhotoCache.setSummary(String.format(Locale.CHINESE, "%.2fMb", photoFileSize));
}
示例11: bindPreference
import android.preference.SwitchPreference; //导入依赖的package包/类
/**
* Binds a preference's summary to its value. More specifically, when the
* preference's value is changed, its summary (line of text below the
* preference title) is updated to reflect the value. The summary is also
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*/
private static void bindPreference(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreference);
// Trigger the listener immediately with the preference's
// current value.
if (preference instanceof SwitchPreference) {
sBindPreference.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getBoolean(preference.getKey(), true));
} else {
sBindPreference.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
}
示例12: onPreferenceChange
import android.preference.SwitchPreference; //导入依赖的package包/类
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
} else if (preference instanceof SwitchPreference) {
// For a boolean value, set the default value "true"
preference.setDefaultValue((stringValue.contains("t")));
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
示例13: bindPreferenceSummaryToValue
import android.preference.SwitchPreference; //导入依赖的package包/类
/**
* Binds a preference's summary to its value. More specifically, when the
* preference's value is changed, its summary (line of text below the
* preference title) is updated to reflect the value. The summary is also
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* @see #sBindPreferenceSummaryToValueListener
*/
private static void bindPreferenceSummaryToValue(@NonNull Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
if (preference instanceof SwitchPreference) {
// Trigger the listener immediately with the preference's current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getBoolean(preference.getKey(), false));
} else {
// Trigger the listener immediately with the preference's current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
}
示例14: checkNotificationsPermission
import android.preference.SwitchPreference; //导入依赖的package包/类
private boolean checkNotificationsPermission(Context c, boolean prompt) {
ContentResolver contentResolver = c.getContentResolver();
String enabledNotificationListeners = Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
String packageName = c.getPackageName();
// check to see if the enabledNotificationListeners String contains our package name
if (enabledNotificationListeners == null || !enabledNotificationListeners.contains(packageName)) {
((SwitchPreference) findPreference("notifications_alerts")).setChecked(false);
if (Utils.isAndroidNewerThanL() && prompt) {
Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
checkAndStartActivity(intent);
shouldEnableNotificationsAlerts = true;
} else if (prompt) {
checkAndStartActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
shouldEnableNotificationsAlerts = true;
}
return false;
}
return true;
}
示例15: onActivityResult
import android.preference.SwitchPreference; //导入依赖的package包/类
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case PasswordActivity.ASK_PWD_REQUEST:
// if I asked for a pwd and the pwd was ok the disable the
// preference, otherwise enable it again
((SwitchPreference) findPreference(getResources().getString(R.string
.pref_key_enablepwd)))
.setChecked(resultCode != Activity.RESULT_OK);
break;
case ChangePasswordActivity.CHANGE_PWD_REQUEST:
// if I ask for a pwd change and the password was changed correctly
// enable the preference
((SwitchPreference) findPreference(getResources().getString(R.string
.pref_key_enablepwd)))
.setChecked(resultCode == Activity.RESULT_OK);
break;
}
}