Android preference SharedPreference-android learning Tour (6)

Source: Internet
Author: User

Android preference SharedPreference-android learning Tour (6)

SharedPrefenence uses key-value pairs for storage and internal storage.

Instance
Public class MainActivity extends Activity {private SharedPreferences sp; private CheckBox cb = null; public static final String KEY_SHOW_DIALOG = "show_dialog"; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // The first parameter is the preference name. If not, it is automatically created. The second parameter is the access method. This parameter can only be accessed by your own program or the same id, another method is to allow multi-process access. Sp = getSharedPreferences ("mysp", Context. MODE_PRIVATE); cb = (CheckBox) findViewById (R. id. cb); cb. setOnCheckedChangeListener (new CompoundButton. onCheckedChangeListener () {@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stub Editor edit = sp. edit (); edit. putBoolean (KEY_SHOW_DIALOG, isChecked); edit. commit () ;}}); cb. setChecked (sp. getBoolean (KEY_SHOW_DIALOG, false); if (cb. isChecked () {new AlertDialog. builder (this ). setTitle ("welcome "). setMessage ("Hello, welcome to use me "). setPositiveButton ("off", null );}}}

If this option is selected, a dialog box is displayed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.