How do you trap when the user clicks on a ListPreference from the menu that pops up?
Can you tell me how to trap when the user clicks on a ListPreference from the menu that pops up? In my app the user can select a volume level. I would like to show some feedback using a Toast after the user clicks on the list such as "Medium vo开发者_如何学Golume has been set" when the user clicks on the Medium list item.
Here is what my settings activity looks like now. Can you show the additional coding needed and where to place that coding?
Thanks.
Truly, Emad
public class ClockSettings extends PreferenceActivity {
/*
* This is called when the class is created. It displays a Settings screen
* from the settings.xml file.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* Read the settings definition from XML and show them in the current
* activity (screen).
*/
addPreferencesFromResource(R.xml.settings);
/*
* Hide this preference since it's used as a flag.
*/
CheckBoxPreference mCheckBoxPref = (CheckBoxPreference) findPreference("PhoneInUse");
PreferenceCategory mCategory = (PreferenceCategory) findPreference("Quarters");
mCategory.removePreference(mCheckBoxPref);
/*
* Get the preferences from the xml file to be used in click listeners.
*/
Preference settingMasterChimeToggle = (Preference) findPreference("MasterChimeToggle");
/*
* The user clicked on the setting for chiming on the hour.
*/
settingMasterChimeToggle
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
/*
* Get all the settings from the settings xml file.
*/
SharedPreferences clockSettings = PreferenceManager
.getDefaultSharedPreferences(ClockSettings.this);
/*
* Load these settings into a variable for testing.
*/
boolean booleanMasterChimeToggle = clockSettings
.getBoolean("MasterChimeToggle", false);
/*
* Adjust the other chime settings based on the state of
* this one.
*/
SharedPreferences.Editor prefEditor = clockSettings
.edit(); // Allow the settings to be changed.
if (booleanMasterChimeToggle == true) {
prefEditor.putBoolean("ChimeOnTheHour", true);
prefEditor.putBoolean("ChimeOn15Past", true);
prefEditor.putBoolean("ChimeOn30Past", true);
prefEditor.putBoolean("ChimeOn45Past", true);
Toast.makeText(ClockSettings.this,
"Full chiming has now been set.",
Toast.LENGTH_SHORT).show();
} else {
prefEditor.putBoolean("ChimeOnTheHour", false);
prefEditor.putBoolean("ChimeOn15Past", false);
prefEditor.putBoolean("ChimeOn30Past", false);
prefEditor.putBoolean("ChimeOn45Past", false);
Toast.makeText(ClockSettings.this,
"All chiming has now been disabled.",
Toast.LENGTH_SHORT).show();
}
prefEditor.commit(); // Save changes.
finishThisActivity();
return true;
}
});
}
/*
* Take user back to the home screen.
*/
private void finishThisActivity() {
this.finish();
}
}
精彩评论