Как вы ловите, когда пользователь нажимает на ListPreference из всплывающего меню? - PullRequest
1 голос
/ 12 сентября 2011

Можете ли вы сказать мне, как перехватить, когда пользователь нажимает на ListPreference из всплывающего меню? В моем приложении пользователь может выбрать уровень громкости. Я хотел бы показать некоторые отзывы с помощью тоста после того, как пользователь нажимает на список, например «Средний объем был установлен», когда пользователь нажимает на элемент среднего списка.

Вот как выглядит моя активность в настройках. Можете ли вы показать дополнительное кодирование, необходимое и где разместить этот код?

Спасибо.

Действительно, Имад

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

}

}

...