Mi sono stancato il cervello tutta la notte ma non riesco a realizzare questa piccola cosa. Vorrei aggiungere uno SwitchPreference nel mio PreferenceActivity di un'app. Sotto c'è una fotoSwitchPreference onChecked/onClick Listener
Prima di dire troppo, il mio problema è esattamente questo: non riesco a mettere un ascoltatore solo sulla parte Switch della preferenza. Sono in grado di impostare un onPreferenceTreeClick e un onPreferenceClick sulla preferenza, e ciò funziona correttamente se premo sulla porzione di testo. Ma quando lo Switch stesso non fa nulla quando lo cambio da OFF a ON.
Ho letto la documentazione su SwitchPreference. Ho anche guardato gli android/packages/Settings e sembra che AOSP usi uno Switch e non uno SwitchPreference per Wi-Fi e Bluetooth.
Ecco il mio tentativo (di lavoro se si preme sull'intero elemento preferenza, ma non se si preme l'interruttore):
Esempio:
public class Preferences extends SherlockPreferenceActivity {
public static final String PREF_THEME = "pref_theme_interface";
public static final String PREF_ROOT = "pref_root";
public static final String PREF_APP = "pref_app";
public static SharedPreferences mTheme;
private static SharedPreferences mUpdate;
public static SharedPreferences.Editor mEditor;
public boolean SDK_COMPAT = true;
boolean pSwitch = false;
boolean update = true;
Preference autoUpdate;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(MainActivity.THEME);
super.onCreate(savedInstanceState);
final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(R.drawable.ic_preferences);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
SDK_COMPAT = false;
}
mUpdate = PreferenceManager.getDefaultSharedPreferences(this);
update = mUpdate.getBoolean("update", false);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
setPreferenceScreen(createPreferenceSDK());
}
private PreferenceScreen createPreferenceSDK() {
// Root
PreferenceScreen root = (PreferenceScreen)findPreference(PREF_ROOT);
PreferenceCategory prefApp = (PreferenceCategory)findPreference(PREF_APP);
//root.addPreference(prefApp);
if (SDK_COMPAT == true) {
pSwitch = true;
autoUpdate = new SwitchPreference(this);
autoUpdate.setKey("auto_update_pref");
autoUpdate.setTitle(R.string.auto_update);
//autoUpdate.setSummary(update == false ? "Disabled" : "Enabled");
prefApp.addPreference(autoUpdate);
} else {
pSwitch = false;
autoUpdate = new CheckBoxPreference(this);
autoUpdate.setKey("auto_update_pref");
autoUpdate.setTitle(R.string.auto_update);
autoUpdate.setSummary(R.string.auto_update_summary);
prefApp.addPreference(autoUpdate);
}
autoUpdate.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
mEditor = mUpdate.edit();
boolean checked = ((SwitchPreference) preference)
.isChecked();
if (checked) {
update = true;
mEditor.putBoolean("update", true);
mEditor.commit();
autoUpdate.setSummary(update == false ? "Disabled" : "Enabled");
} else {
update = false;
mEditor.putBoolean("update", false);
mEditor.commit();
autoUpdate.setSummary(update == false ? "Disabled" : "Enabled");
}
return true;
}
});
return root;
}
Quindi, per ribadire la mia domanda nel caso in cui ti ho perso. Come si imposta un ascoltatore sulla porzione Switch di SwitchPreference? Per favore sii gentile se è qualcosa di così ovvio. Era piuttosto tardi la scorsa notte quando ho provato ad aggiungere questo.
Grazie mille in anticipo.
Note: 1. Non sono contrario a rispettare CheckBoxPreference, ma preferisco utilizzare Switch perché è bello.
- Sì, lo so che è più facile/migliore? modo di aggiungere preferenze dinamiche usando res/xml e res/xml-v14 invece di fare il controllo SDK. L'ho appena fatto per i test.
EDIT
Speriamo che questo aiuta qualcun altro! Grazie Tushar per suggerimento :-)
autoUpdate.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
boolean switched = ((SwitchPreference) preference)
.isChecked();
update = !switched;
mEditor = mUpdate.edit();
mEditor.putBoolean("update", update);
mEditor.commit();
autoUpdate.setSummary(update == false ? "Disabled" : "Enabled");
return true;
}
});
Hai provato a usare setOnPreferenceChangeListener invece? – Tushar
Tushar, in realtà non l'ho provato. Lasciami fare un tentativo. Grazie. – tommytomatoe
dimmi come va. Sono curioso. – Tushar