In the Android system, there are various storage methods, and SharedPreference is the simplest one.
SharedPreference is essentially an xml file, and its storage structure is a Map. The values in it are key-value pairs. Such:
We can see that the root node isElement. Each element below is the stored value corresponding to different basic attributes. sharedpreference only supports these simple basic attributes. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel/kernel + zb/J0tS9q8/kernel + kernel/kernel + ajuw.vcd4kpha + kernel + 1nq958pacc + hozwvcD4KPHA tprc68jnz8kjuw.vcd4kpha + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java; "> public class MainActivity extends Activity implements OnClickListener {... private SharedPreferences sharedPreferences; private SharedPreferences. editor editor ;... @ Overrideprotected void onCreate (Bundle savedInstanceState ){... sharedPreferences = this. getSharedPreferences (ACCOUNTS, MODE_PRIVATE); editor = sharedPreferences. edit ();}... private boolean write () {username = etUsername. getText (). toString (); password = etPassword. getText (). toString (); editor. putString (USERNAME, username); editor. putString (PASSWORD, password); editor. putBoolean ("boolean", true); editor. putFloat ("float", 2.0f); editor. putInt ("int", 1000); editor. putLong ("Long", 1234567890l); editor. commit ();...}}
Let's take a look at the usage of sharedpreference:
1) Use this. getSharedPreferences to obtain an object:
1.1) the first parameter is the name of the file. When we save the file, it will be placed in the path of data/package name/shared_prefs:
1.2) The second parameter is a Mode, which generally uses the following three values:
A) MODE_PRIVATE: indicates that the file is private. The created file can only be accessed by the current application.
B) MODE_WORLD_READABLE: It is readable in the world. Obviously, other applications can access it, but only read it.
C) MODE_WORLD_WRITABLE: the world can be written, which is more risky. Other applications can not only read but also write (overwrite ).
2) if we only need to read the value, for example, in WelcomeActivity, we only need to read the value in it, as long as we get this object.
public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_welcome);sharedPreferences = this.getSharedPreferences(MainActivity.ACCOUNTS, MODE_PRIVATE);tvWlcUser = (TextView) findViewById(R.id.tvWlcUser);String username = sharedPreferences.getString(MainActivity.USERNAME, "Mr. Unknown");tvWlcUser.setText(username);}
Above, we can call sharedpreference. getXXX and other methods to obtain the value of the corresponding type. The first parameter is the Key value, and the second parameter is the default value given when the value is not obtained.
3) when we want to write values into the file, such as in the first code, we need an editor object. In fact, all write value operations are written through the editor object. It is like a transaction and finally calls the editor. the commit () method can write data to a file and save it in the document written above.
The following is the login interface. Enter your username and password, and click login to go to the welcome page. Click the source code!