你能告诉我当用户从弹出的菜单中点击 ListPreference 时如何设置陷阱吗?在我的应用程序中,用户可以选择音量级别。当用户单击“中”列表项时,我想在用户单击列表后使用 Toast 显示一些反馈,例如“已设置中音量”。
这是我的设置活动现在的样子。您能否展示所需的额外编码以及该编码的放置位置?
谢谢。
真的,埃马德
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();
}
}