Back to project page DataPersistence.
The source code is released under:
Apache License
If you think the Android project DataPersistence listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.datapersistencedemo; // w w w .j a va 2s .com import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; public class SharedPreferencesActivity extends Activity { private EditText accountText,passwordText; private CheckBox rememberCheck; private Button login; private SharedPreferences pref; private Editor editor; private boolean isRemember; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sharedpreferences); init(); //??????????????????????????????????????????? isRemember = pref.getBoolean("remember", false); if (isRemember) { //??????????????????????? accountText.setText(pref.getString("account", "")); passwordText.setText(pref.getString("password", "")); rememberCheck.setChecked(true); } login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { editor = pref.edit(); if (rememberCheck.isChecked()) { editor.putString("account", accountText.getText().toString()); editor.putString("password", passwordText.getText().toString()); editor.putBoolean("remember", true); }else { editor.clear(); } editor.commit(); } }); } private void init() { accountText = (EditText) findViewById(R.id.account); passwordText = (EditText) findViewById(R.id.password); login = (Button) findViewById(R.id.login); rememberCheck = (CheckBox) findViewById(R.id.remember_password); pref = PreferenceManager.getDefaultSharedPreferences(this); } }