1 page.title= 2 page.tags=preference,preferenceactivity,preferencefragment 3 4 @jd:body 5 6 7 <div id="qv-wrapper"> 8 <div id="qv"> 9 10 <h2> </h2> 11 <ol> 12 <li><a href="#Overview"></a> 13 <ol> 14 <li><a href="#SettingTypes"> </a></li> 15 </ol> 16 </li> 17 <li><a href="#DefiningPrefs">XML </a> 18 <ol> 19 <li><a href="#Groups"> </a></li> 20 <li><a href="#Intents"> </a></li> 21 </ol> 22 </li> 23 <li><a href="#Activity"> </a></li> 24 <li><a href="#Fragment"> </a></li> 25 <li><a href="#Defaults"> </a></li> 26 <li><a href="#PreferenceHeaders"> </a> 27 <ol> 28 <li><a href="#CreateHeaders"> </a></li> 29 <li><a href="#DisplayHeaders"> </a></li> 30 <li><a href="#BackCompatHeaders"> </a></li> 31 </ol> 32 </li> 33 <li><a href="#ReadingPrefs"> </a> 34 <ol> 35 <li><a href="#Listening"> </a></li> 36 </ol> 37 </li> 38 <li><a href="#NetworkUsage"> </a></li> 39 <li><a href="#Custom"> </a> 40 <ol> 41 <li><a href="#CustomSelected"> </a></li> 42 <li><a href="#CustomSave"> </a></li> 43 <li><a href="#CustomInitialize"> </a></li> 44 <li><a href="#CustomDefault"> </a></li> 45 <li><a href="#CustomSaveState"> </a></li> 46 </ol> 47 </li> 48 </ol> 49 50 <h2>Key </h2> 51 <ol> 52 <li>{@link android.preference.Preference}</li> 53 <li>{@link android.preference.PreferenceActivity}</li> 54 <li>{@link android.preference.PreferenceFragment}</li> 55 </ol> 56 57 58 <h2> </h2> 59 <ol> 60 <li><a href="{@docRoot}design/patterns/settings.html"> </a></li> 61 </ol> 62 </div> 63 </div> 64 65 66 67 68 <p> . 69 70 .</p> 71 72 <p> , Android 73 {@link android.preference.Preference} API Android ( ) 74 . 75 {@link android.preference.Preference} API .</p> 76 77 <div class="note design"> 78 <p><strong> </strong></p> 79 <p> <a href="{@docRoot}design/patterns/settings.html"></a> .</p> 80 </div> 81 82 83 <img src="{@docRoot}images/ui/settings/settings.png" alt="" width="435" /> 84 <p class="img-caption"><strong> 1.</strong> Android 85 . {@link android.preference.Preference} 86 .</p> 87 88 89 90 91 <h2 id="Overview"></h2> 92 93 <p> {@link android.view.View} , 94 {@link android.preference.Preference} . 95 XML .</p> 96 97 <p>{@link android.preference.Preference} 98 . {@link android.preference.Preference} 99 UI . {@link 100 android.preference.CheckBoxPreference} , {@link 101 android.preference.ListPreference} .</p> 102 103 <p> {@link android.preference.Preference} - 104 {@link android.content.SharedPreferences} 105 . 106 {@link android.content.SharedPreferences} . 107 {@link android.content.SharedPreferences} 108 .</p> 109 110 <p> {@link android.content.SharedPreferences} 111 .</p> 112 113 <ul> 114 <li>Boolean</li> 115 <li>Float</li> 116 <li>Int</li> 117 <li>Long</li> 118 <li>String</li> 119 <li>String {@link java.util.Set}</li> 120 </ul> 121 122 <p> UI 123 {@link android.view.View} 124 {@link android.preference.Preference} , {@link android.app.Activity} 125 {@link android.app.Fragment} .</p> 126 127 <ul> 128 <li> Android 3.0 (API 10 ) , 129 {@link android.preference.PreferenceActivity} .</li> 130 <li>Android 3.0 {@link android.app.Activity} 131 . {@link android.preference.PreferenceFragment} . 132 , {@link android.preference.PreferenceActivity} 133 .</li> 134 </ul> 135 136 <p>{@link android.preference.PreferenceActivity} {@link 137 android.preference.PreferenceFragment} <a href="#Activity"> </a> <a href="#Fragment"> 138 </a> .</p> 139 140 141 <h3 id="SettingTypes"> </h3> 142 143 <p> {@link 144 android.preference.Preference} . 145 . 146 . , 1 147 . {@link 148 android.preference.Preference} .</p> 149 150 <p> .</p> 151 152 <dl> 153 <dt>{@link android.preference.CheckBoxPreference}</dt> 154 <dd> . 155 ( <code>true</code>).</dd> 156 157 <dt>{@link android.preference.ListPreference}</dt> 158 <dd> . 159 ( ) .</dd> 160 161 <dt>{@link android.preference.EditTextPreference}</dt> 162 <dd>{@link android.widget.EditText} . {@link 163 java.lang.String}.</dd> 164 </dl> 165 166 <p> {@link android.preference.Preference} 167 .</p> 168 169 <p> 170 . {@link 171 android.preference.Preference} . 172 {@link android.preference.Preference} . <a href="#Custom"> </a> .</p> 173 174 175 176 <h2 id="DefiningPrefs">XML </h2> 177 178 <p> {@link android.preference.Preference} , 179 {@link android.preference.Preference} 180 XML . XM 181 . , 182 . .</p> 183 184 <p> {@link android.preference.Preference} XML 185 . {@code <CheckBoxPreference>} .</p> 186 187 <p> XML {@code res/xml/} . , 188 {@code preferences.xml} . . 189 ( ) 190 {@link android.preference.PreferenceScreen} .</p> 191 192 <p class="note"><strong>:</strong> , 193 XML .</p> 194 195 <p>XML {@link android.preference.PreferenceScreen 196 <PreferenceScreen>} . {@link 197 android.preference.Preference} . 198 {@link android.preference.PreferenceScreen <PreferenceScreen>} 199 .</p> 200 201 <p>:</p> 202 203 <pre> 204 <?xml version="1.0" encoding="utf-8"?> 205 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 206 <CheckBoxPreference 207 android:key="pref_sync" 208 android:title="@string/pref_sync" 209 android:summary="@string/pref_sync_summ" 210 android:defaultValue="true" /> 211 <ListPreference 212 android:dependency="pref_sync" 213 android:key="pref_syncConnectionType" 214 android:title="@string/pref_syncConnectionType" 215 android:dialogTitle="@string/pref_syncConnectionType" 216 android:entries="@array/pref_syncConnectionTypes_entries" 217 android:entryValues="@array/pref_syncConnectionTypes_values" 218 android:defaultValue="@string/pref_syncConnectionTypes_default" /> 219 </PreferenceScreen> 220 </pre> 221 222 <p> {@link android.preference.CheckBoxPreference} {@link 223 android.preference.ListPreference} . .</p> 224 225 <dl> 226 <dt>{@code android:key}</dt> 227 <dd> . () 228 , {@link 229 android.content.SharedPreferences} . 230 <p> <em> </em> 231 {@link android.preference.PreferenceCategory} {@link android.preference.PreferenceScreen} , 232 {@link android.content.Intent} (<a href="#Intents">{@code <intent>}</a> ) {@link android.app.Fragment} (<a href="{@docRoot}reference/android/preference/Preference.html#attr_android:fragment">{@code 233 android:fragment}</a> ).</p> 234 </dd> 235 <dt>{@code android:title}</dt> 236 <dd> .</dd> 237 <dt>{@code android:defaultValue}</dt> 238 <dd> {@link 239 android.content.SharedPreferences} . 240 .</dd> 241 </dl> 242 243 <p> {@link 244 android.preference.Preference}( ) .</p> 245 246 247 <div class="figure" style="width:300px"> 248 <img src="{@docRoot}images/ui/settings/settings-titles.png" alt="" /> 249 <p class="img-caption"><strong> 2.</strong> 250 . <br/><b>1.</b> {@link 251 android.preference.PreferenceCategory <PreferenceCategory>} . <br/><b>2.</b> 252 {@code android:title} .</p> 253 </div> 254 255 256 <p> 10 257 , 258 . .</p> 259 260 261 <h3 id="Groups"> </h3> 262 263 <p>10 , 264 . 265 266 . .</p> 267 268 <ul> 269 <li><a href="#Titles"> </a></li> 270 <li><a href="#Subscreens"> </a></li> 271 </ul> 272 273 <p> . 274 , Android 275 <a href="{@docRoot}design/patterns/settings.html"></a> .</p> 276 277 278 <h4 id="Titles"> </h4> 279 280 <p> ( 2 ), 281 {@link android.preference.Preference} {@link 282 android.preference.PreferenceCategory} .</p> 283 284 <p>:</p> 285 286 <pre> 287 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 288 <PreferenceCategory 289 android:title="@string/pref_sms_storage_title" 290 android:key="pref_key_storage_settings"> 291 <CheckBoxPreference 292 android:key="pref_key_auto_delete" 293 android:summary="@string/pref_summary_auto_delete" 294 android:title="@string/pref_title_auto_delete" 295 android:defaultValue="false"... /> 296 <Preference 297 android:key="pref_key_sms_delete_limit" 298 android:dependency="pref_key_auto_delete" 299 android:summary="@string/pref_summary_delete_limit" 300 android:title="@string/pref_title_sms_delete"... /> 301 <Preference 302 android:key="pref_key_mms_delete_limit" 303 android:dependency="pref_key_auto_delete" 304 android:summary="@string/pref_summary_delete_limit" 305 android:title="@string/pref_title_mms_delete" ... /> 306 </PreferenceCategory> 307 ... 308 </PreferenceScreen> 309 </pre> 310 311 312 <h4 id="Subscreens"> </h4> 313 314 <p> ( 3 ), 315 {@link android.preference.Preference} {@link 316 android.preference.PreferenceScreen} .</p> 317 318 <img src="{@docRoot}images/ui/settings/settings-subscreen.png" alt="" /> 319 <p class="img-caption"><strong> 3.</strong> . {@code 320 <PreferenceScreen>} 321 , .</p> 322 323 <p>:</p> 324 325 <pre> 326 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 327 <!-- opens a subscreen of settings --> 328 <PreferenceScreen 329 android:key="button_voicemail_category_key" 330 android:title="@string/voicemail" 331 android:persistent="false"> 332 <ListPreference 333 android:key="button_voicemail_provider_key" 334 android:title="@string/voicemail_provider" ... /> 335 <!-- opens another nested subscreen --> 336 <PreferenceScreen 337 android:key="button_voicemail_setting_key" 338 android:title="@string/voicemail_settings" 339 android:persistent="false"> 340 ... 341 </PreferenceScreen> 342 <RingtonePreference 343 android:key="button_voicemail_ringtone_key" 344 android:title="@string/voicemail_ringtone_title" 345 android:ringtoneType="notification" ... /> 346 ... 347 </PreferenceScreen> 348 ... 349 </PreferenceScreen> 350 </pre> 351 352 353 <h3 id="Intents"> </h3> 354 355 <p> 356 . . {@link 357 android.content.Intent} , {@code <intent>} 358 {@code <Preference>} .</p> 359 360 <p> .</p> 361 362 <pre> 363 <Preference android:title="@string/prefs_web_page" > 364 <intent android:action="android.intent.action.VIEW" 365 android:data="http://www.example.com" /> 366 </Preference> 367 </pre> 368 369 <p> .</p> 370 371 <dl> 372 <dt>{@code android:action}</dt> 373 <dd> , {@link android.content.Intent#setAction setAction()} 374 .</dd> 375 <dt>{@code android:data}</dt> 376 <dd> , {@link android.content.Intent#setData setData()} .</dd> 377 <dt>{@code android:mimeType}</dt> 378 <dd> MIME , {@link android.content.Intent#setType setType()} 379 .</dd> 380 <dt>{@code android:targetClass}</dt> 381 <dd> , {@link android.content.Intent#setComponent 382 setComponent()} .</dd> 383 <dt>{@code android:targetPackage}</dt> 384 <dd> , {@link 385 android.content.Intent#setComponent setComponent()} .</dd> 386 </dl> 387 388 389 390 <h2 id="Activity"> </h2> 391 392 <p> {@link 393 android.preference.PreferenceActivity} . {@link 394 android.app.Activity} . {@link 395 android.preference.Preference} . {@link android.preference.PreferenceActivity} 396 {@link 397 android.preference.Preference} .</p> 398 399 <p class="note"><strong>:</strong> Android 3.0 , 400 {@link android.preference.PreferenceFragment} . 401 <a href="#Fragment"> </a> .</p> 402 403 <p> {@link 404 android.preference.PreferenceActivity#onCreate onCreate()} . {@link 405 android.preference.PreferenceActivity#addPreferencesFromResource addPreferencesFromResource()} 406 XML . 407 {@link android.preference.PreferenceActivity} .</p> 408 409 <pre> 410 public class SettingsActivity extends PreferenceActivity { 411 @Override 412 public void onCreate(Bundle savedInstanceState) { 413 super.onCreate(savedInstanceState); 414 addPreferencesFromResource(R.xml.preferences); 415 } 416 } 417 </pre> 418 419 <p> . 420 {@link android.content.SharedPreferences} , 421 . 422 . 423 {@link android.content.SharedPreferences} 424 <a href="#ReadingPrefs"> </a> .</p> 425 426 427 428 429 <h2 id="Fragment"> </h2> 430 431 <p>Android 3.0(API 11) , {@link 432 android.preference.PreferenceFragment} {@link android.preference.Preference} 433 . {@link android.preference.PreferenceFragment} . , 434 {@link android.preference.PreferenceActivity} .</p> 435 436 <p><a href="{@docRoot}guide/components/fragments.html"></a> 437 , 438 . {@link 439 android.preference.PreferenceFragment} {@link 440 android.preference.PreferenceActivity} ( ).</p> 441 442 <p>{@link android.preference.PreferenceFragment} . 443 {@link android.preference.PreferenceFragment#onCreate onCreate()} 444 {@link android.preference.PreferenceFragment#addPreferencesFromResource 445 addPreferencesFromResource()} . :</p> 446 447 <pre> 448 public static class SettingsFragment extends PreferenceFragment { 449 @Override 450 public void onCreate(Bundle savedInstanceState) { 451 super.onCreate(savedInstanceState); 452 453 // Load the preferences from an XML resource 454 addPreferencesFromResource(R.xml.preferences); 455 } 456 ... 457 } 458 </pre> 459 460 <p> {@link android.app.Activity} , 461 {@link android.app.Fragment} . :</p> 462 463 <pre> 464 public class SettingsActivity extends Activity { 465 @Override 466 protected void onCreate(Bundle savedInstanceState) { 467 super.onCreate(savedInstanceState); 468 469 // Display the fragment as the main content. 470 getFragmentManager().beginTransaction() 471 .replace(android.R.id.content, new SettingsFragment()) 472 .commit(); 473 } 474 } 475 </pre> 476 477 <p class="note"><strong>:</strong> {@link android.preference.PreferenceFragment} 478 {@link android.content.Context} . {@link android.content.Context} 479 , {@link android.app.Fragment#getActivity()} . , 480 {@link android.app.Fragment#getActivity()} . 481 , {@link 482 android.app.Fragment#getActivity()} null .</p> 483 484 485 <h2 id="Defaults"> </h2> 486 487 <p> . 488 {@link android.content.SharedPreferences} 489 {@link android.preference.Preference} 490 .</p> 491 492 <p> XML {@link 493 android.preference.Preference} 494 . {@code android:defaultValue} . 495 {@link android.preference.Preference} . : 496 </p> 497 498 <pre> 499 <!-- default value is a boolean --> 500 <CheckBoxPreference 501 android:defaultValue="true" 502 ... /> 503 504 <!-- default value is a string --> 505 <ListPreference 506 android:defaultValue="@string/pref_syncConnectionTypes_default" 507 ... /> 508 </pre> 509 510 <p> , {@link android.app.Activity#onCreate onCreate()} 511 — 512 —{@link android.preference.PreferenceManager#setDefaultValues 513 setDefaultValues()} .</p> 514 515 <pre> 516 PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false); 517 </pre> 518 519 <p> {@link android.app.Activity#onCreate onCreate()} 520 . 521 ( 522 ).</p> 523 524 <p> .</p> 525 <ul> 526 <li> {@link android.content.Context}.</li> 527 <li> XML ID.</li> 528 <li> . 529 <p><code>false</code> , 530 ( {@link android.preference.PreferenceManager#KEY_HAS_SET_DEFAULT_VALUES} 531 ).</p></li> 532 </ul> 533 534 <p> <code>false</code> 535 , 536 . <code>true</code> , 537 .</p> 538 539 540 541 <h2 id="PreferenceHeaders"> </h2> 542 543 <p> 544 <a href="#Subscreens"> </a> (: , 545 4 5 ). Android 3.0 , Android 3.0 546 "" . 547 {@link android.preference.PreferenceScreen} .</p> 548 549 <p> .</p> 550 <ol> 551 <li> {@link 552 android.preference.PreferenceFragment} . , XML 553 .</li> 554 <li> XML 555 .</li> 556 <li>{@link android.preference.PreferenceActivity} .</li> 557 <li>{@link 558 android.preference.PreferenceActivity#onBuildHeaders onBuildHeaders()} 559 .</li> 560 </ol> 561 562 <p> {@link android.preference.PreferenceActivity} 563 () 4 .</p> 564 565 <p> Android 3.0 566 {@link android.preference.PreferenceFragment} 567 568 (<a href="#BackCompatHeaders"> 569 </a> ).</p> 570 571 <img src="{@docRoot}images/ui/settings/settings-headers-tablet.png" alt="" /> 572 <p class="img-caption"><strong> 4.</strong> . <br/><b>1.</b> 573 XML . <br/><b>2.</b> 574 {@link android.preference.PreferenceFragment} , {@code <header>} 575 .</p> 576 577 <img src="{@docRoot}images/ui/settings/settings-headers-handset.png" alt="" /> 578 <p class="img-caption"><strong> 5.</strong> . 579 {@link android.preference.PreferenceFragment} 580 .</p> 581 582 583 <h3 id="CreateHeaders" style="clear:left"> </h3> 584 585 <p> {@code <preference-headers>} 586 {@code <header>} . :</p> 587 588 <pre> 589 <?xml version="1.0" encoding="utf-8"?> 590 <preference-headers xmlns:android="http://schemas.android.com/apk/res/android"> 591 <header 592 android:fragment="com.example.prefs.SettingsActivity$SettingsFragmentOne" 593 android:title="@string/prefs_category_one" 594 android:summary="@string/prefs_summ_category_one" /> 595 <header 596 android:fragment="com.example.prefs.SettingsActivity$SettingsFragmentTwo" 597 android:title="@string/prefs_category_two" 598 android:summary="@string/prefs_summ_category_two" > 599 <!-- key/value pairs can be included as arguments for the fragment. --> 600 <extra android:name="someKey" android:value="someHeaderValue" /> 601 </header> 602 </preference-headers> 603 </pre> 604 605 <p> {@code android:fragment} {@link 606 android.preference.PreferenceFragment} .</p> 607 608 <p>{@code <extras>} - {@link 609 android.os.Bundle} . {@link 610 android.app.Fragment#getArguments()} . , 611 {@link 612 android.preference.PreferenceFragment} , 613 XML .</p> 614 615 <p> . 616 {@code "settings"} {@code <extra>} .</p> 617 618 <pre> 619 public static class SettingsFragment extends PreferenceFragment { 620 @Override 621 public void onCreate(Bundle savedInstanceState) { 622 super.onCreate(savedInstanceState); 623 624 String settings = getArguments().getString("settings"); 625 if ("notifications".equals(settings)) { 626 addPreferencesFromResource(R.xml.settings_wifi); 627 } else if ("sync".equals(settings)) { 628 addPreferencesFromResource(R.xml.settings_sync); 629 } 630 } 631 } 632 </pre> 633 634 635 636 <h3 id="DisplayHeaders"> </h3> 637 638 <p> {@link 639 android.preference.PreferenceActivity#onBuildHeaders onBuildHeaders()} 640 {@link android.preference.PreferenceActivity#loadHeadersFromResource 641 loadHeadersFromResource()} . :</p> 642 643 <pre> 644 public class SettingsActivity extends PreferenceActivity { 645 @Override 646 public void onBuildHeaders(List<Header> target) { 647 loadHeadersFromResource(R.xml.preference_headers, target); 648 } 649 } 650 </pre> 651 652 <p> {@link 653 android.preference.PreferenceFragment} .</p> 654 655 <p class="note"><strong>:</strong> , {@link 656 android.preference.PreferenceActivity} {@link 657 android.preference.PreferenceActivity#onCreate onCreate()} . 658 .</p> 659 660 661 <h3 id="BackCompatHeaders"> </h3> 662 663 <p> Android 3.0 664 Android 3.0 . XML 665 . {@link android.preference.Preference 666 <Preference>} ( Android 667 ).</p> 668 669 <p> {@link android.preference.PreferenceScreen} {@link 670 android.preference.Preference <Preference>} {@link android.content.Intent} 671 {@link android.preference.PreferenceActivity} . XML 672 .</p> 673 674 <p> Android 3.0 675 XML ({@code res/xml/preference_headers.xml}).</p> 676 677 <pre> 678 <preference-headers xmlns:android="http://schemas.android.com/apk/res/android"> 679 <header 680 android:fragment="com.example.prefs.SettingsFragmentOne" 681 android:title="@string/prefs_category_one" 682 android:summary="@string/prefs_summ_category_one" /> 683 <header 684 android:fragment="com.example.prefs.SettingsFragmentTwo" 685 android:title="@string/prefs_category_two" 686 android:summary="@string/prefs_summ_category_two" /> 687 </preference-headers> 688 </pre> 689 690 <p> , Android 3.0 691 ({@code res/xml/preference_headers_legacy.xml}).</p> 692 693 <pre> 694 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 695 <Preference 696 android:title="@string/prefs_category_one" 697 android:summary="@string/prefs_summ_category_one" > 698 <intent 699 android:targetPackage="com.example.prefs" 700 android:targetClass="com.example.prefs.SettingsActivity" 701 android:action="com.example.prefs.PREFS_ONE" /> 702 </Preference> 703 <Preference 704 android:title="@string/prefs_category_two" 705 android:summary="@string/prefs_summ_category_two" > 706 <intent 707 android:targetPackage="com.example.prefs" 708 android:targetClass="com.example.prefs.SettingsActivity" 709 android:action="com.example.prefs.PREFS_TWO" /> 710 </Preference> 711 </PreferenceScreen> 712 </pre> 713 714 <p>{@code <preference-headers>} Android 3.0 715 {@link android.preference.PreferenceActivity#onBuildHeaders onBuildHeaders()} {@link 716 android.preference.PreferenceActivity} Android 3.0 . "" 717 ({@code preference_headers_legacy.xml}) Android 718 , Android 3.0 ({@link 719 android.os.Build.VERSION_CODES#HONEYCOMB}), {@link 720 android.preference.PreferenceActivity#addPreferencesFromResource addPreferencesFromResource()} 721 . :</p> 722 723 <pre> 724 @Override 725 public void onCreate(Bundle savedInstanceState) { 726 super.onCreate(savedInstanceState); 727 ... 728 729 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 730 // Load the legacy preferences headers 731 addPreferencesFromResource(R.xml.preference_headers_legacy); 732 } 733 } 734 735 // Called only on Honeycomb and later 736 @Override 737 public void onBuildHeaders(List<Header> target) { 738 loadHeadersFromResource(R.xml.preference_headers, target); 739 } 740 </pre> 741 742 <p> {@link android.content.Intent} . 743 . XML 744 {@code <intent>} .</p> 745 746 <pre> 747 final static String ACTION_PREFS_ONE = "com.example.prefs.PREFS_ONE"; 748 ... 749 750 @Override 751 public void onCreate(Bundle savedInstanceState) { 752 super.onCreate(savedInstanceState); 753 754 String action = getIntent().getAction(); 755 if (action != null && action.equals(ACTION_PREFS_ONE)) { 756 addPreferencesFromResource(R.xml.preferences); 757 } 758 ... 759 760 else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 761 // Load the legacy preferences headers 762 addPreferencesFromResource(R.xml.preference_headers_legacy); 763 } 764 } 765 </pre> 766 767 <p>{@link 768 android.preference.PreferenceActivity#addPreferencesFromResource addPreferencesFromResource()} 769 . 'Else-if' 770 .</p> 771 772 773 774 775 776 <h2 id="ReadingPrefs"> </h2> 777 778 <p> 779 {@link 780 android.preference.PreferenceManager#getDefaultSharedPreferences 781 PreferenceManager.getDefaultSharedPreferences()} . {@link 782 android.content.SharedPreferences} , {@link 783 android.preference.PreferenceActivity} {@link android.preference.Preference} 784 - .</p> 785 786 <p> 787 .</p> 788 789 <pre> 790 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); 791 String syncConnPref = sharedPref.getString(SettingsActivity.KEY_PREF_SYNC_CONN, ""); 792 </pre> 793 794 795 796 <h3 id="Listening"> </h3> 797 798 <p> 799 . , 800 {@link android.content.SharedPreferences.OnSharedPreferenceChangeListener 801 SharedPreference.OnSharedPreferenceChangeListener} 802 {@link android.content.SharedPreferences} . {@link 803 android.content.SharedPreferences#registerOnSharedPreferenceChangeListener 804 registerOnSharedPreferenceChangeListener()} .</p> 805 806 <p> {@link 807 android.content.SharedPreferences.OnSharedPreferenceChangeListener#onSharedPreferenceChanged 808 onSharedPreferenceChanged()} , 809 . :</p> 810 811 <pre> 812 public class SettingsActivity extends PreferenceActivity 813 implements OnSharedPreferenceChangeListener { 814 public static final String KEY_PREF_SYNC_CONN = "pref_syncConnectionType"; 815 ... 816 817 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, 818 String key) { 819 if (key.equals(KEY_PREF_SYNC_CONN)) { 820 Preference connectionPref = findPreference(key); 821 // Set summary to be the user-description for the selected value 822 connectionPref.setSummary(sharedPreferences.getString(key, "")); 823 } 824 } 825 } 826 </pre> 827 828 <p> . 829 {@link android.preference.PreferenceActivity#findPreference findPreference()} 830 {@link android.preference.Preference} , 831 . , {@link 832 android.preference.ListPreference} , {@link 833 android.preference.Preference#setSummary setSummary()} ( 834 5 ).</p> 835 836 <p class="note"><strong>:</strong> Android <a href="{@docRoot}design/patterns/settings.html"></a> , 837 {@link android.preference.ListPreference} . 838 .</p> 839 840 <p> 841 {@link android.content.SharedPreferences.OnSharedPreferenceChangeListener} {@link 842 android.app.Activity#onResume} {@link android.app.Activity#onPause} .</p> 843 844 <pre> 845 @Override 846 protected void onResume() { 847 super.onResume(); 848 getPreferenceScreen().getSharedPreferences() 849 .registerOnSharedPreferenceChangeListener(this); 850 } 851 852 @Override 853 protected void onPause() { 854 super.onPause(); 855 getPreferenceScreen().getSharedPreferences() 856 .unregisterOnSharedPreferenceChangeListener(this); 857 } 858 </pre> 859 860 <p class="caution"><strong>:</strong> {@link 861 android.content.SharedPreferences#registerOnSharedPreferenceChangeListener 862 registerOnSharedPreferenceChangeListener()} 863 , . 864 . . 865 . 866 .</p> 867 868 <p> 869 . 870 .</p> 871 872 <pre> 873 prefs.registerOnSharedPreferenceChangeListener( 874 // Bad! The listener is subject to garbage collection! 875 new SharedPreferences.OnSharedPreferenceChangeListener() { 876 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { 877 // listener implementation 878 } 879 }); 880 </pre> 881 882 <p>, 883 .</p> 884 885 <pre> 886 SharedPreferences.OnSharedPreferenceChangeListener listener = 887 new SharedPreferences.OnSharedPreferenceChangeListener() { 888 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { 889 // listener implementation 890 } 891 }; 892 prefs.registerOnSharedPreferenceChangeListener(listener); 893 </pre> 894 895 <h2 id="NetworkUsage"> </h2> 896 897 898 <p>Android 4.0 899 . 900 . 901 902 .<p> 903 904 <p> . Wi-Fi 905 / , . 906 907 . 908 .</p> 909 910 <p> {@link android.preference.PreferenceActivity} 911 , {@link 912 android.content.Intent#ACTION_MANAGE_NETWORK_USAGE} . :</p> 913 914 <pre> 915 <activity android:name="SettingsActivity" ... > 916 <intent-filter> 917 <action android:name="android.intent.action.MANAGE_NETWORK_USAGE" /> 918 <category android:name="android.intent.category.DEFAULT" /> 919 </intent-filter> 920 </activity> 921 </pre> 922 923 <p> 924 . , 925 <em> </em> 926 {@link android.preference.PreferenceActivity} . 927 .</p> 928 929 930 931 932 933 934 935 <h2 id="Custom"> </h2> 936 937 <p>Android {@link android.preference.Preference} 938 UI . 939 , . 940 . . 941 {@link android.preference.Preference} .</p> 942 943 <p>{@link android.preference.Preference} , 944 .</p> 945 946 <ul> 947 <li> .</li> 948 <li> .</li> 949 <li>{@link android.preference.Preference} 950 ( ) .</li> 951 <li> .</li> 952 <li>{@link android.preference.Preference} UI(: ) , 953 (: ).</li> 954 </ul> 955 956 <p> .</p> 957 958 959 960 <h3 id="CustomSelected"> </h3> 961 962 <p>{@link android.preference.Preference} , 963 {@link android.preference.Preference#onClick()} 964 . , {@link android.preference.DialogPreference} 965 . . {@link 966 android.preference.DialogPreference} {@link 967 android.preference.DialogPreference#setDialogLayoutResource setDialogLayoutResourcs()} 968 .</p> 969 970 <p> {@link 971 android.preference.DialogPreference} 972 .</p> 973 974 <pre> 975 public class NumberPickerPreference extends DialogPreference { 976 public NumberPickerPreference(Context context, AttributeSet attrs) { 977 super(context, attrs); 978 979 setDialogLayoutResource(R.layout.numberpicker_dialog); 980 setPositiveButtonText(android.R.string.ok); 981 setNegativeButtonText(android.R.string.cancel); 982 983 setDialogIcon(null); 984 } 985 ... 986 } 987 </pre> 988 989 990 991 <h3 id="CustomSave"> </h3> 992 993 <p> . {@link 994 android.preference.Preference} {@code persist*()} . {@link 995 android.preference.Preference#persistInt persistInt()}, 996 {@link android.preference.Preference#persistBoolean persistBoolean()} .</p> 997 998 <p class="note"><strong>:</strong> {@link android.preference.Preference} 999 , 1000 {@link android.preference.Preference} {@code persist*()} .</p> 1001 1002 <p> {@link 1003 android.preference.Preference} . {@link 1004 android.preference.DialogPreference} 1005 ( "(OK)" ).</p> 1006 1007 <p>{@link android.preference.DialogPreference} {@link 1008 android.preference.DialogPreference#onDialogClosed onDialogClosed()} . 1009 "" . 1010 <code>true</code> , . : 1011 </p> 1012 1013 <pre> 1014 @Override 1015 protected void onDialogClosed(boolean positiveResult) { 1016 // When the user selects "OK", persist the new value 1017 if (positiveResult) { 1018 persistInt(mNewValue); 1019 } 1020 } 1021 </pre> 1022 1023 <p> <code>mNewValue</code> 1024 . {@link android.preference.Preference#persistInt persistInt()} 1025 {@link android.content.SharedPreferences} ( 1026 {@link android.preference.Preference} XML ).</p> 1027 1028 1029 <h3 id="CustomInitialize"> </h3> 1030 1031 <p> {@link android.preference.Preference} , 1032 {@link android.preference.Preference#onSetInitialValue onSetInitialValue()} 1033 . , 1034 .</p> 1035 1036 <p>{@link android.preference.Preference#onSetInitialValue onSetInitialValue()} 1037 <code>restorePersistedValue</code> 1038 . <code>true</code>, 1039 {@link 1040 android.preference.Preference} {@code getPersisted*()} . {@link 1041 android.preference.Preference#getPersistedInt getPersistedInt()} . 1042 , UI 1043 .</p> 1044 1045 <p><code>restorePersistedValue</code> <code>false</code> , 1046 .</p> 1047 1048 <pre> 1049 @Override 1050 protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) { 1051 if (restorePersistedValue) { 1052 // Restore existing state 1053 mCurrentValue = this.getPersistedInt(DEFAULT_VALUE); 1054 } else { 1055 // Set default state from the XML attribute 1056 mCurrentValue = (Integer) defaultValue; 1057 persistInt(mCurrentValue); 1058 } 1059 } 1060 </pre> 1061 1062 <p> {@code getPersisted*()} 1063 . 1064 {@link 1065 android.preference.Preference#getPersistedInt getPersistedInt()} .</p> 1066 1067 <p class="caution"><strong>:</strong> {@code getPersisted*()} 1068 <code>defaultValue</code> <strong> </strong>. 1069 <code>restorePersistedValue</code> <code>true</code> null .</p> 1070 1071 1072 <h3 id="CustomDefault"> </h3> 1073 1074 <p>{@link android.preference.Preference} 1075 ({@code android:defaultValue} ), 1076 {@link android.preference.Preference#onGetDefaultValue 1077 onGetDefaultValue()} . 1078 {@link 1079 android.content.SharedPreferences} . :</p> 1080 1081 <pre> 1082 @Override 1083 protected Object onGetDefaultValue(TypedArray a, int index) { 1084 return a.getInteger(index, DEFAULT_VALUE); 1085 } 1086 </pre> 1087 1088 <p> . 1089 {@code android:defaultValue} , . 1090 , 1091 .</p> 1092 1093 1094 1095 <h3 id="CustomSaveState"> </h3> 1096 1097 <p> {@link android.view.View} {@link android.preference.Preference} 1098 1099 ( ). 1100 {@link android.preference.Preference} , 1101 {@link android.preference.Preference#onSaveInstanceState 1102 onSaveInstanceState()} {@link 1103 android.preference.Preference#onRestoreInstanceState onRestoreInstanceState()} .</p> 1104 1105 <p>{@link android.preference.Preference} 1106 {@link android.os.Parcelable} . Android 1107 . {@link 1108 android.preference.Preference.BaseSavedState} .</p> 1109 1110 <p>{@link android.preference.Preference} 1111 {@link android.preference.Preference.BaseSavedState} . 1112 {@link android.preference.Preference.BaseSavedState#CREATOR} 1113 .</p> 1114 1115 <p> , 1116 {@code value} . {@link android.preference.Preference} 1117 .</p> 1118 1119 <pre> 1120 private static class SavedState extends BaseSavedState { 1121 // Member that holds the setting's value 1122 // Change this data type to match the type saved by your Preference 1123 int value; 1124 1125 public SavedState(Parcelable superState) { 1126 super(superState); 1127 } 1128 1129 public SavedState(Parcel source) { 1130 super(source); 1131 // Get the current preference's value 1132 value = source.readInt(); // Change this to read the appropriate data type 1133 } 1134 1135 @Override 1136 public void writeToParcel(Parcel dest, int flags) { 1137 super.writeToParcel(dest, flags); 1138 // Write the preference's value 1139 dest.writeInt(value); // Change this to write the appropriate data type 1140 } 1141 1142 // Standard creator object using an instance of this class 1143 public static final Parcelable.Creator<SavedState> CREATOR = 1144 new Parcelable.Creator<SavedState>() { 1145 1146 public SavedState createFromParcel(Parcel in) { 1147 return new SavedState(in); 1148 } 1149 1150 public SavedState[] newArray(int size) { 1151 return new SavedState[size]; 1152 } 1153 }; 1154 } 1155 </pre> 1156 1157 <p> {@link android.preference.Preference.BaseSavedState} 1158 ( {@link android.preference.Preference} ), 1159 {@link android.preference.Preference#onSaveInstanceState 1160 onSaveInstanceState()} {@link 1161 android.preference.Preference#onRestoreInstanceState onRestoreInstanceState()} . 1162 {@link android.preference.Preference} .</p> 1163 1164 <p>:</p> 1165 1166 <pre> 1167 @Override 1168 protected Parcelable onSaveInstanceState() { 1169 final Parcelable superState = super.onSaveInstanceState(); 1170 // Check whether this Preference is persistent (continually saved) 1171 if (isPersistent()) { 1172 // No need to save instance state since it's persistent, 1173 // use superclass state 1174 return superState; 1175 } 1176 1177 // Create instance of custom BaseSavedState 1178 final SavedState myState = new SavedState(superState); 1179 // Set the state's value with the class member that holds current 1180 // setting value 1181 myState.value = mNewValue; 1182 return myState; 1183 } 1184 1185 @Override 1186 protected void onRestoreInstanceState(Parcelable state) { 1187 // Check whether we saved the state in onSaveInstanceState 1188 if (state == null || !state.getClass().equals(SavedState.class)) { 1189 // Didn't save the state, so call superclass 1190 super.onRestoreInstanceState(state); 1191 return; 1192 } 1193 1194 // Cast state to custom BaseSavedState and pass to superclass 1195 SavedState myState = (SavedState) state; 1196 super.onRestoreInstanceState(myState.getSuperState()); 1197 1198 // Set this Preference's widget to reflect the restored state 1199 mNumberPicker.setValue(myState.value); 1200 } 1201 </pre> 1202 1203