Android Application Development Basics (9)-----sharedpreferences

Source: Internet
Author: User
<span id="Label3"></p><p><p>Link Address: http://www.cnblogs.com/lknlfy/archive/2012/02/27/2370319.html</p></p><p><p><strong>I. Overview</strong></p></p><p><p>For sharedpreferences, I'm it. It is understood as a simple database that can store data like a file in the system or read it, but it only stores some simple numerical pairs, such as the Key-values form. If some apps need to save some simple data on exit to restore the data they need to be opened again, you can do so with Sharedpreferences.</p></p><p><p></p></p><p><p><strong>second, The requirements</strong></p></p><p><p>Write a simple app that requires the app to save some of the data it needs when it exits, and restore the data when the app is opened Again.</p></p><p><p></p></p><p><p><strong>third, the realization</strong></p></p><p><p>New Project myshared, Modify The/res/layout/main.xml file, Add a edittext in it, two buttons and a textview, complete the Main.xml file as Follows:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>1 <?xml version= "1.0" encoding= "utf-8"?><br>2 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"<br>3 android:layout_width= "fill_parent"<br>4 android:layout_height= "fill_parent"<br>5 android:orientation= "vertical" ><br>6<br>7 <edittext<br>8 android:id= "@+id/edittext"<br>9 Android:layout_width= "fill_parent"<br>Ten android:layout_height= "wrap_content"<br>android:hint= "please Enter content"<br>/><br>13<br><button<br>Android:id= "@+id/wbutton"<br>Android:layout_width= "fill_parent"<br>android:layout_height= "wrap_content"<br>android:text= "write"<br>/><br>20<br><button<br>Android:id= "@+id/rbutton"<br>Android:layout_width= "fill_parent"<br>android:layout_height= "wrap_content"<br>android:text= "read"<br>/><br>27<br><textview<br>Android:id= "@+id/textview"<br>Android:layout_width= "fill_parent"<br>android:layout_height= "wrap_content"<br>android:gravity= "center_horizontal"<br>Android:textsize= "20dip"<br>/><br>35<br></LinearLayout></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Then Modify the Mysharedactivity.java file, the main definition of a Sharedpreferences object and a sharedpreferences.editor object, the implementation of two buttons to listen, the complete Mysharedactivity.java file as Follows:</p></p><p><p></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>1 package com.nan.shared;<br>2<br>3 Import android.app.Activity;<br>4 Import android.content.SharedPreferences;<br>5 Import android.os.Bundle;<br>6 Import android.view.View;<br>7 Import android.widget.Button;<br>8 Import android.widget.EditText;<br>9 Import android.widget.TextView;<br>Ten Import android.widget.Toast;<br>11<br>public class Mysharedactivity extends Activity<br>13 {<br>The private EditText Medittext = null;<br>The private Button Writebutton = null;<br>Private Button Readbutton = null;<br>+ Private TextView Mtextview = null;<br>18<br>Private Sharedpreferences msharedpreferences = null;<br>Private Sharedpreferences.editor Meditor = null;<br>21st<br>/** called when the activity is first Created. */<br>@Override<br>public void OnCreate (Bundle Savedinstancestate)<br>25 {<br>Super.oncreate (savedinstancestate);<br>Setcontentview (r.layout.main);<br>28<br>Medittext = (EditText) This.findviewbyid (r.id.edittext);<br>Writebutton = (Button) This.findviewbyid (r.id.wbutton);<br>Readbutton = (Button) This.findviewbyid (r.id.rbutton);<br>Mtextview = (TextView) This.findviewbyid (r.id.textview);<br>33<br>msharedpreferences = getsharedpreferences ("myshare", mode_private);<br>Meditor = Msharedpreferences.edit ();<br>36<br>Panax Notoginseng writebutton.setonclicklistener (new view.onclicklistener ()<br>38 {<br>39<br>@Override<br>$ public void OnClick (View V)<br>42 {<br>+//TODO auto-generated Method Stub<br>44//get Edittext's Content<br>String Text = Medittext.gettext (). toString ();<br>46//modify<br>Meditor.putstring ("hehe", text);<br>48//submit changes<br>Meditor.commit ();<br>Displaytoast ("write success! ");<br>51}<br>52});<br>53<br>Readbutton.setonclicklistener (new View.onclicklistener ()<br>55 {<br>56<br>@Override<br>(+) public void OnClick (View V)<br>59 {<br>/TODO auto-generated Method Stub<br>61//get the saved data<br>String Text = msharedpreferences.getstring ("hehe", null);<br>63//show the data<br>Mtextview.settext (text);<br>65}<br>66});<br>67<br>68}<br>69//show Toast function<br>+ private void Displaytoast (String S)<br>71 {<br>Toast.maketext (mysharedactivity.this, s, toast.length_short). show ();<br>73}<br>74<br>75}</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>well, Run it, as Follows:</p></p><p><p></p></p><p><p></p></p><p><p>Then enter a line of numbers and click "write", as Follows:</p></p><p><p></p></p><p><p></p></p><p><p>To exit the app, rerun it and click on the "read" button, as Follows:</p></p><p><p></p></p><p><p></p></p><p><p>You can see that the previously saved data is Displayed.</p></p><p><p>OK, Finish.</p></p><p><p>Android Application Development Basics (9)-----sharedpreferences</p></p></span>

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.