深圳全飞鸿

标题: SettingsActivity [打印本页]

作者: zhgc    时间: 2019-5-22 20:57
标题: SettingsActivity
  1. package com.example.administrator.myapplicationc;

  2. import android.annotation.TargetApi;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.res.Configuration;
  6. import android.media.Ringtone;
  7. import android.media.RingtoneManager;
  8. import android.net.Uri;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.preference.ListPreference;
  12. import android.preference.Preference;
  13. import android.preference.PreferenceActivity;
  14. import android.support.v7.app.ActionBar;
  15. import android.preference.PreferenceFragment;
  16. import android.preference.PreferenceManager;
  17. import android.preference.RingtonePreference;
  18. import android.text.TextUtils;
  19. import android.view.MenuItem;

  20. import java.util.List;

  21. /**
  22. * A {@link PreferenceActivity} that presents a set of application settings. On
  23. * handset devices, settings are presented as a single list. On tablets,
  24. * settings are split by category, with category headers shown to the left of
  25. * the list of settings.
  26. * <p>
  27. * See <a href="http://developer.android.com/design/patterns/settings.html">
  28. * Android Design: Settings</a> for design guidelines and the <a
  29. * href="http://developer.android.com/guide/topics/ui/settings.html">Settings
  30. * API Guide</a> for more information on developing a Settings UI.
  31. */
  32. public class SettingsActivity extends AppCompatPreferenceActivity {

  33.     /**
  34.      * A preference value change listener that updates the preference's summary
  35.      * to reflect its new value.
  36.      */
  37.     private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
  38.         @Override
  39.         public boolean onPreferenceChange(Preference preference, Object value) {
  40.             String stringValue = value.toString();

  41.             if (preference instanceof ListPreference) {
  42.                 // For list preferences, look up the correct display value in
  43.                 // the preference's 'entries' list.
  44.                 ListPreference listPreference = (ListPreference) preference;
  45.                 int index = listPreference.findIndexOfValue(stringValue);

  46.                 // Set the summary to reflect the new value.
  47.                 preference.setSummary(
  48.                         index >= 0
  49.                                 ? listPreference.getEntries()[index]
  50.                                 : null);

  51.             } else if (preference instanceof RingtonePreference) {
  52.                 // For ringtone preferences, look up the correct display value
  53.                 // using RingtoneManager.
  54.                 if (TextUtils.isEmpty(stringValue)) {
  55.                     // Empty values correspond to 'silent' (no ringtone).
  56.                     preference.setSummary(R.string.pref_ringtone_silent);

  57.                 } else {
  58.                     Ringtone ringtone = RingtoneManager.getRingtone(
  59.                             preference.getContext(), Uri.parse(stringValue));

  60.                     if (ringtone == null) {
  61.                         // Clear the summary if there was a lookup error.
  62.                         preference.setSummary(null);
  63.                     } else {
  64.                         // Set the summary to reflect the new ringtone display
  65.                         // name.
  66.                         String name = ringtone.getTitle(preference.getContext());
  67.                         preference.setSummary(name);
  68.                     }
  69.                 }

  70.             } else {
  71.                 // For all other preferences, set the summary to the value's
  72.                 // simple string representation.
  73.                 preference.setSummary(stringValue);
  74.             }
  75.             return true;
  76.         }
  77.     };

  78.     /**
  79.      * Helper method to determine if the device has an extra-large screen. For
  80.      * example, 10" tablets are extra-large.
  81.      */
  82.     private static boolean isXLargeTablet(Context context) {
  83.         return (context.getResources().getConfiguration().screenLayout
  84.                 & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
  85.     }

  86.     /**
  87.      * Binds a preference's summary to its value. More specifically, when the
  88.      * preference's value is changed, its summary (line of text below the
  89.      * preference title) is updated to reflect the value. The summary is also
  90.      * immediately updated upon calling this method. The exact display format is
  91.      * dependent on the type of preference.
  92.      *
  93.      * @see #sBindPreferenceSummaryToValueListener
  94.      */
  95.     private static void bindPreferenceSummaryToValue(Preference preference) {
  96.         // Set the listener to watch for value changes.
  97.         preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

  98.         // Trigger the listener immediately with the preference's
  99.         // current value.
  100.         sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
  101.                 PreferenceManager
  102.                         .getDefaultSharedPreferences(preference.getContext())
  103.                         .getString(preference.getKey(), ""));
  104.     }

  105.     @Override
  106.     protected void onCreate(Bundle savedInstanceState) {
  107.         super.onCreate(savedInstanceState);
  108.         setupActionBar();
  109.     }

  110.     /**
  111.      * Set up the {@link android.app.ActionBar}, if the API is available.
  112.      */
  113.     private void setupActionBar() {
  114.         ActionBar actionBar = getSupportActionBar();
  115.         if (actionBar != null) {
  116.             // Show the Up button in the action bar.
  117.             actionBar.setDisplayHomeAsUpEnabled(true);
  118.         }
  119.     }

  120.     /**
  121.      * {@inheritDoc}
  122.      */
  123.     @Override
  124.     public boolean onIsMultiPane() {
  125.         return isXLargeTablet(this);
  126.     }

  127.     /**
  128.      * {@inheritDoc}
  129.      */
  130.     @Override
  131.     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  132.     public void onBuildHeaders(List<Header> target) {
  133.         loadHeadersFromResource(R.xml.pref_headers, target);
  134.     }

  135.     /**
  136.      * This method stops fragment injection in malicious applications.
  137.      * Make sure to deny any unknown fragments here.
  138.      */
  139.     protected boolean isValidFragment(String fragmentName) {
  140.         return PreferenceFragment.class.getName().equals(fragmentName)
  141.                 || GeneralPreferenceFragment.class.getName().equals(fragmentName)
  142.                 || DataSyncPreferenceFragment.class.getName().equals(fragmentName)
  143.                 || NotificationPreferenceFragment.class.getName().equals(fragmentName);
  144.     }

  145.     /**
  146.      * This fragment shows general preferences only. It is used when the
  147.      * activity is showing a two-pane settings UI.
  148.      */
  149.     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  150.     public static class GeneralPreferenceFragment extends PreferenceFragment {
  151.         @Override
  152.         public void onCreate(Bundle savedInstanceState) {
  153.             super.onCreate(savedInstanceState);
  154.             addPreferencesFromResource(R.xml.pref_general);
  155.             setHasOptionsMenu(true);

  156.             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
  157.             // to their values. When their values change, their summaries are
  158.             // updated to reflect the new value, per the Android Design
  159.             // guidelines.
  160.             bindPreferenceSummaryToValue(findPreference("example_text"));
  161.             bindPreferenceSummaryToValue(findPreference("example_list"));
  162.         }

  163.         @Override
  164.         public boolean onOptionsItemSelected(MenuItem item) {
  165.             int id = item.getItemId();
  166.             if (id == android.R.id.home) {
  167.                 startActivity(new Intent(getActivity(), SettingsActivity.class));
  168.                 return true;
  169.             }
  170.             return super.onOptionsItemSelected(item);
  171.         }
  172.     }

  173.     /**
  174.      * This fragment shows notification preferences only. It is used when the
  175.      * activity is showing a two-pane settings UI.
  176.      */
  177.     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  178.     public static class NotificationPreferenceFragment extends PreferenceFragment {
  179.         @Override
  180.         public void onCreate(Bundle savedInstanceState) {
  181.             super.onCreate(savedInstanceState);
  182.             addPreferencesFromResource(R.xml.pref_notification);
  183.             setHasOptionsMenu(true);

  184.             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
  185.             // to their values. When their values change, their summaries are
  186.             // updated to reflect the new value, per the Android Design
  187.             // guidelines.
  188.             bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
  189.         }

  190.         @Override
  191.         public boolean onOptionsItemSelected(MenuItem item) {
  192.             int id = item.getItemId();
  193.             if (id == android.R.id.home) {
  194.                 startActivity(new Intent(getActivity(), SettingsActivity.class));
  195.                 return true;
  196.             }
  197.             return super.onOptionsItemSelected(item);
  198.         }
  199.     }

  200.     /**
  201.      * This fragment shows data and sync preferences only. It is used when the
  202.      * activity is showing a two-pane settings UI.
  203.      */
  204.     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  205.     public static class DataSyncPreferenceFragment extends PreferenceFragment {
  206.         @Override
  207.         public void onCreate(Bundle savedInstanceState) {
  208.             super.onCreate(savedInstanceState);
  209.             addPreferencesFromResource(R.xml.pref_data_sync);
  210.             setHasOptionsMenu(true);

  211.             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
  212.             // to their values. When their values change, their summaries are
  213.             // updated to reflect the new value, per the Android Design
  214.             // guidelines.
  215.             bindPreferenceSummaryToValue(findPreference("sync_frequency"));
  216.         }

  217.         @Override
  218.         public boolean onOptionsItemSelected(MenuItem item) {
  219.             int id = item.getItemId();
  220.             if (id == android.R.id.home) {
  221.                 startActivity(new Intent(getActivity(), SettingsActivity.class));
  222.                 return true;
  223.             }
  224.             return super.onOptionsItemSelected(item);
  225.         }
  226.     }
  227. }
复制代码





欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/) Powered by Discuz! X3.2